SQL SELECT那里是B

时间:2017-09-29 07:48:30

标签: mysql sql

查询1:

SELECT * FROM `games` WHERE `title` LIKE '%{$search}%'

查询2:

SELECT * FROM `games` WHERE `tags` LIKE '%{$search}%'

我想在这样的一个中获得两个查询:

SELECT * FROM `games` WHERE `title` LIKE '%{$search}%' then `tags` LIKE '%{$search}%'

我获得Title =搜索词的游戏的搜索结果优先级First, 然后显示标签=搜索词的不太重要的结果。

怎么做?

1 个答案:

答案 0 :(得分:1)

case中设置ORDER BY表达式,以便首先返回标题:

SELECT *
FROM `games`
WHERE `title` LIKE '%{$search}%' OR `tags` LIKE '%{$search}%'
ORDER BY case when `title` LIKE '%{$search}%' then 0 else 1 end