是否可以根据 WHERE 子句中的匹配数对 MYSQL 查询的结果进行排序 (DESC), 已被多个 OR 运算符分隔?
我想对以下查询执行此操作:
SELECT column_1, column_2, column_2
FROM tbl_table
WHERE column_1 LIKE '%$string1%'
OR column_2 LIKE '%$string2%'
OR column_3 LIKE '%$string3%'
ORDER BY ... "
最佳答案
您可以这样使用 SUM() 和 GROUP BY:
SELECT column_1, column_2, column_3, SUM(IF(column_1 LIKE '%$string1%', 1, 0)) as S1, SUM(IF(column_2 LIKE '%$string2%', 1, 0)) as S2, SUM(IF(column_3 LIKE '%$string3%', 1, 0)) as S3 FROM tbl_table WHERE column_1 LIKE '%$string1%' OR column_2 LIKE '%$string2%' OR column_3 LIKE '%$string3%' GROUP BY column_1, column_2, column_3 ORDER BY S1 DESC, S2 DESC, S3 DESC
关于php - Mysql:根据 WHERE 子句中的匹配数对结果进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31924165/