如何在 MySQL 中仅一次订购返回重复列值?
要在 MySQL 中仅返回一次列值,让我们先看一个示例并创建一个表 -
mysql> create table DemoTable680(Status varchar(100)); Query OK, 0 rows affected (0.75 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable680 values('Busy'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable680 values('At Work'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable680 values('Busy'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable680 values('Blocked'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable680 values('Offline'); Query OK, 1 row affected (0.67 sec) mysql> insert into DemoTable680 values('Offline'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable680 values('Blocked'); Query OK, 1 row affected (0.14 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable680;
这将会产生以下输出 -
+---------+ | Status | +---------+ | Busy | | At Work | | Busy | | Blocked | | Offline | | Offline | | Blocked | +---------+ 7 rows in set (0.00 sec)
以下是在 MySQL 中仅返回重复列值的查询 -
mysql> select distinct Status from DemoTable680 order by Status;
这将会产生以下输出 -
+---------+ | Status | +---------+ | At Work | | Blocked | | Busy | | Offline | +---------+ 4 rows in set (0.00 sec)
广告