- 在查询数据的时候,数据库里面查询出来的数据有多行是重复的数据我们需要剔除,就可以用到 distinct 关键字,这个关键字需要在查询的字段前面添加。
- 注意:必须是查询结果中存在1条或多条完全一样数据的才会被剔除,如果多行里面查询出来的除了id不一样,其他数据一样也不行,除非去掉id进行查询,下面就有例子:
select distinct 查询字段,查询字段 from 表名;
mysql> select * from test; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | | 2 | xyq | 20 | | 3 | xyq | 20 | +----+------+------+ mysql> select name, age from test; +------+------+ | name | age | +------+------+ | dzm | 20 | | xyq | 20 | | xyq | 20 | +------+------+ mysql> select distinct name, age from test; +------+------+ | name | age | +------+------+ | dzm | 20 | | xyq | 20 | +------+------+ mysql> select distinct id, name, age from test; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | | 2 | xyq | 20 | | 3 | xyq | 20 | +----+------+------+