简体   繁体   English

在MySQL中首先显示包含特定值的记录

[英]Show records that contain a certain value first in MySQL

I need the records in my database that are "featured" to be listed first. 我需要在我的数据库中首先列出“特色”的记录。 If a listing is "featured" the value in the featured column is "yes". 如果列表是“特色”,则特色列中的值为“是”。

I am not sure what kind of MySQL Query would give me this result, or if it even exists. 我不确定什么样的MySQL Query会给我这个结果,或者它是否存在。 But the other idea I have is to have one query that gets all featured ones and lists them, and then another one gets all of the listings that aren't featured. 但我的另一个想法是让一个查询获得所有特色的并列出它们,然后另一个查询获得所有未列出的列表。

Do you have any ideas? 你有什么想法? Thanks in Advance! 提前致谢!

Use an ORDER BY with a CASE statement, as in 将ORDER BY与CASE语句一起使用,如

SELECT * 
FROM TheTable
ORDER BY CASE LOWER(Featured)
           WHEN 'yes' THEN 0 
           ELSE 1 
         END 
         ASC,
         SomeOtherColumnNameForAMinorKeySort ASC

EDIT : Renamed RecordName to SomeOtherColumnNameForAMinorKeySort to better express what the column's purpose is. 编辑 :将RecordName重命名为SomeOtherColumnNameForAMinorKeySort,以更好地表达列的用途。

SELECT fields FROM table ORDER BY featured DESC;

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

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