【发布时间】:2014-11-27 19:00:43
【问题描述】:
我有一个格式为
的数据库| Field | Type | Null | Key | Default | Extra |
| word | varchar(60) | NO | | NULL | |
| english_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| word | english_id |
| the | 1 |
| of | 2 |
| and | 3 |
| a | 4 |
| to | 5 |
| in | 6 |
| ant | 7 |
| be | 8 |
| that | 9 |
| was | 10 |
我希望能够按输入的顺序进行排序,但也可以保留重复项。因此,如果他们输入"a cat and a dog",我会通过下面的 SQL 查询得到正确的顺序。问题是,由于我使用a 两次,第二个a 没有出现。如何在输出中按顺序保留重复值?这是当前查询。
select english_id, word from english where word in ("Here", "are", "are", "dogs")
order by CASE word
WHEN "here" then 1
WHEN "are" then 2
WHEN "are" then 3
WHEN "dogs" then 4
END;
这是它的输出
| english_id | word |
| 131 | here |
| 26 | are |
| 1679 | dogs |
我希望它看起来像这样
| english_id | word |
| 131 | here |
| 26 | are |
| 26 | are |
| 1679 | dogs |
【问题讨论】:
标签: mysql sql case sql-order-by