维护在MySQL中传递的ID的自定义顺序
要维持ID的自定义顺序,请使用ORDER BY CASE语句。让我们首先创建一个表-
mysql> create table DemoTable1550-> (
-> Id int,
-> Name varchar(20)
-> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1550 values(101,'Chris');mysql> insert into DemoTable1550 values(110,'Bob');
mysql> insert into DemoTable1550 values(105,'Carol');
mysql> insert into DemoTable1550 values(109,'Mike');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1550;
这将产生以下输出-
+------+-------+| Id | Name |
+------+-------+
| 101 | Chris |
| 110 | Bob |
| 105 | Carol |
| 109 | Mike |
+------+-------+
4 rows in set (0.00 sec)
这是维护传递的ID顺序的查询-
mysql> select * from DemoTable1550-> where Id IN(109,101,110)
-> order by case Id
-> when 109 then 1001
-> when 101 then 1002
-> when 110 then 1003
-> end;
这将产生以下输出-
+------+-------+| Id | Name |
+------+-------+
| 109 | Mike |
| 101 | Chris |
| 110 | Bob |
+------+-------+
3 rows in set (0.00 sec)
以上是 维护在MySQL中传递的ID的自定义顺序 的全部内容, 来源链接: utcz.com/z/338309.html