简体   繁体   English

SQL选择A然后B

[英]SQL SELECT WHERE A THEN B

Query 1: 查询1:

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

Query 2: 查询2:

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

I want to get both 2 queries in one like this: 我想在一个这样的两个查询:

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

I get priority of search result for games with Title = search word First, then showing less-important results for tags = search word. 我首先使用Title = search word来获得游戏搜索结果的优先级,然后显示tag = search word不太重要的结果。

How to do this? 这个怎么做?

Have a case expression in the ORDER BY to return title matches first: ORDER BY有一个case表达式,以首先返回标题匹配项:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM