1. 程式人生 > >distinct 去除重複

distinct 去除重複

mysql> alter table one add column rtime int;    //新增一個時間欄位 
Query OK, 8 rows affected (0.01 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql> desc one;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(50)      | NO   |     | NULL    |                |
| password | varchar(32)      | NO   |     | NULL    |                |
| age      | int(11)          | NO   |     | NULL    |                |
| rtime    | int(11)          | YES  |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

mysql> truncate one;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into one (username,password,age,rtime) values('zhang1',123,25,201
),('zhang2',123,24,2018);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into one (username,password,age,rtime) values('zhang3',123,24,201
),('zhang4',123,24,2018);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into one (username,password,age,rtime) values('zhang4',123,24,201
),('zhang6',123,23,2018);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into one (username,password,age,rtime) values('zhang7',123,21,201
),('zhang8',123,22,2018);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0       //兩條插入
 
mysql> select * from one;
+----+----------+----------+-----+-------+
| id | username | password | age | rtime |
+----+----------+----------+-----+-------+
|  1 | zhang1   | 123      |  25 |  2018 |
|  2 | zhang2   | 123      |  24 |  2018 |
|  3 | zhang3   | 123      |  24 |  2018 |
|  4 | zhang4   | 123      |  24 |  2018 |
|  5 | zhang4   | 123      |  24 |  2018 |
|  6 | zhang6   | 123      |  23 |  2018 |
|  7 | zhang7   | 123      |  21 |  2018 |
|  8 | zhang8   | 123      |  22 |  2018 |
+----+----------+----------+-----+-------+
8 rows in set (0.00 sec)

mysql> select age from one;                   //C查詢出one表所有年齡         
+-----+
| age |
+-----+
|  25 |
|  24 |
|  24 |
|  24 |
|  24 |
|  23 |
|  21 |
|  22 |
+-----+
8 rows in set (0.00 sec)
                                            //distinct 去重 
mysql> select distinct age from one;        //查詢出所有使用者的年齡段去重 
+-----+
| age |
+-----+
|  25 |
|  24 |
|  23 |
|  21 |
|  22 |
+-----+
5 rows in set (0.00 sec)
                                        //all 全部 
mysql> select all age from one;         //查詢所有使用者的所有年齡欄位 (預設為all)
+-----+
| age |
+-----+
|  25 |
|  24 |
|  24 |
|  24 |
|  24 |
|  23 |
|  21 |
|  22 |
+-----+
8 rows in set (0.00 sec) 
 
mysql> select id,username,age from one;   //查詢出所有id username age 欄位 內容
+----+----------+-----+
| id | username | age |
+----+----------+-----+
|  1 | zhang1   |  25 |
|  2 | zhang2   |  24 |
|  3 | zhang3   |  24 |
|  4 | zhang4   |  24 |
|  5 | zhang4   |  24 |
|  6 | zhang6   |  23 |
|  7 | zhang7   |  21 |
|  8 | zhang8   |  22 |
+----+----------+-----+
8 rows in set (0.00 sec)