mysql - SQL - 按 id 列表部分排序

标签 mysql sql sql-order-by

我有一个要查询的表,条件类似于 id >10,我想根据列表中的 id 添加条件,例如 AND id IN(3 ,4,5,6,7).

我想首先按列表中的 id 对结果进行排序。 可以吗?

最佳答案

如果您使用 MySQL:(请适当标记问题)

来自manual :

FIELD(str,str1,str2,str3,...)

Returns the index (position) of str in the str1, str2, str3, ... list. Returns 0 if str is not found.

If all arguments to FIELD() are strings, all arguments are compared as strings. If all arguments are numbers, they are compared as numbers. Otherwise, the arguments are compared as double.

If str is NULL, the return value is 0 because NULL fails equality comparison with any value. FIELD() is the complement of ELT().

mysql> SELECT FIELD('ej', 'Hej', 'ej', 'Heja', 'hej', 'foo');
        -> 2
mysql> SELECT FIELD('fo', 'Hej', 'ej', 'Heja', 'hej', 'foo');
        -> 0

这样你就可以轻松做到

SELECT
...
ORDER BY FIELD(id, 3,4,5,6,7), id

关于mysql - SQL - 按 id 列表部分排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33693845/

相关文章:

sql - 在表中创建与特定模式匹配的列

sql - 是什么导致了这些看似不一致的查询结果?

mysql - 使用 mySQL 按顺序检索值并减去行

vb.net - VB.NET的LINQ中ORDERBY的语法是什么?

php - 选择两个日期之间的值以及要在其间检查的另一个值

sql - 时间戳比 MySQL 中的 datetime 列快多少?

mysql - 如何配置自定义 MySQL NHibernate Batcher?

mysql - 查看我的查询计划,此查询是否会执行(解释)

mysql - 从两个表中选择多个 where 条件和多个 order 子句

java - 具有多个条目的属性的数据库表设计(例如 : Person -> Social Media)