按案例和编号订购

时间:2018-10-22 05:17:20

标签: mysql sql-order-by case


这是我的查询:

SELECT * FROM `players` 
WHERE `team` = '$team' 
ORDER BY `spot` ASC, 
CASE WHEN 
`spot` LIKE '%LW%' THEN '1' 
WHEN `spot` LIKE '%C%' THEN '2' 
WHEN `spot` LIKE '%RW%' THEN '3' 
END

我的目标是通过以下方式订购

  • 1LW
  • 1C
  • 1RW
  • 2LW
  • 2C
  • 2RW

我正在努力弄清楚如何实现这一目标。这是我的桌子的样子: table

1 个答案:

答案 0 :(得分:0)

尝试以下

SELECT * FROM `players` 
WHERE `team` = '$team' 
ORDER BY cast(spot as unsigned),
CASE WHEN 
`spot` LIKE '%LW%' THEN '1' 
WHEN `spot` LIKE '%C%' THEN '2' 
WHEN `spot` LIKE '%RW%' THEN '3' 
END