如何在 MySQL 中对末尾的特定值进行排序?

mysqlmysqli database

为此,您可以使用 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)

相关文章