在MySQL中,我的sql就像是
SELECT * , IF( `Word` = 'sim', 1, IF( `Word` LIKE 'sim%', 2, IF( `Word` LIKE '%sim', 4, 3 ) ) ) AS `sort`
FROM `dblist`
WHERE `Word` LIKE '%sim%'
ORDER BY `sort` , `Word`
这个sql在SQlite中不起作用。我想做结果订单。
SELECT * FROM dblist where word like 'sim' or word like 'sim%' or word like '%sim%' or word like '%sim'
相等的sim是frist,sim%是第二个,%sim%是thrid,然后%sim是最后一个。
目前我不能在sqlite中像mysql那样排序。如何更改sql以订购结果?
答案 0 :(得分:3)
您应该使用CASE expression而不是IF,因为SQLite不支持后者。这是您需要的SQL:
SELECT
*,
CASE WHEN `Word` = 'sim' THEN 1
WHEN `Word` LIKE 'sim%' THEN 2
WHEN `Word` LIKE '%sim' THEN 4
ELSE 3
END `sort`
FROM `dblist`
WHERE `Word` LIKE '%sim%'
ORDER BY `sort` , `Word`
答案 1 :(得分:1)
SELECT * , case when `Word` = 'sim' then 1 else case when `Word` LIKE 'sim%' then 2 else case when `Word` LIKE '%sim' then 4 else 3 end end end as sort
FROM `dblist`
WHERE `Word` LIKE '%sim%'
ORDER BY `sort` , `Word