如何将一个特定值排序到 MySQL 中的末尾?
可使用 ORDER BY 实现此功能。让我们创建一个表 −
mysql> create table demo57 −> ( −> id int not null auto_increment primary key, −> full_name varchar(20) −> ); Query OK, 0 rows affected (1.60 sec)
利用 insert 命令将一些记录插入表中 −
mysql> insert into demo57(full_name) values('John Smith'); Query OK, 1 row affected (0.24 sec) mysql> insert into demo57(full_name) values('David Miller'); Query OK, 1 row affected (0.13 sec) mysql> insert into demo57(full_name) values('Not Known'); Query OK, 1 row affected (0.13 sec) mysql> insert into demo57(full_name) values('Chris Brown'); Query OK, 1 row affected (0.31 sec)
使用 select 语句从表中显示记录 −
mysql> select *from demo57;
将生成以下输出 −
+----+--------------+ | id | full_name | +----+--------------+ | 1 | John Smith | | 2 | David Miller | | 3 | Not Known | | 4 | Chris Brown | +----+--------------+ 4 rows in set (0.00 sec)
以下是对某个特定值进行末尾排序的查询。
mysql> select *from demo57 −> order by(full_name="Not Known"),full_name desc;
将生成以下输出 −
+----+--------------+ | id | full_name | +----+--------------+ | 1 | John Smith | | 2 | David Miller | | 4 | Chris Brown | | 3 | Not Known | +----+--------------+ 4 rows in set (0.00 sec)
广告