2

我有一张桌子,上面有一个订单,如下所示:

Product | Color | Size | Size Range ID (from a different, joined table)
-----------------------------------------------------------------------
Shirt   | Blue  |  L   |   1
Shirt   | Blue  |  M   |   1
Shirt   | Blue  |  S   |   1

请注意,尺寸在上表中按字母顺序排序。

我还有一个尺寸范围表,如下所示:

id | size_01 | size_02 | size_03
--------------------------------
 1 |    S    |   M     |   L  

如何强制第一个表上的项目按第二个表中尺寸的位置排序?

4

2 回答 2

1

您可以使用join组合表格,然后field()获取列列表中的位置:

select i.*
from items i left outer join
     SizeRange sr
     on i.SizeRangeId = sr.SizeRangeId
order by field(i.Size, sr.size_01, sr.size_02, sr.size_03)
于 2013-08-30T18:53:36.293 回答
0
SELECT
  MAX(CASE Size WHEN 'S' THEN Size ELSE '' END) AS 'size_01',
  MAX(CASE Size WHEN 'L' THEN Size ELSE '' END) AS 'size_02',
  MAX(CASE Size WHEN 'M' THEN Size ELSE '' END) AS 'size_03'
FROM Test
GROUP BY Product
于 2013-08-30T19:06:05.467 回答