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)   

             

猜你喜欢

转载自blog.csdn.net/feiyucity/article/details/84754973