在表中,可能会包含重复值。这并不成问题,不过,有时您也许但愿仅仅列出不一样(distinct)的值。关键词 distinct用于返回惟一不一样的值。mysql
表A: 表B:sql
select distinct name from A
执行后结果以下:spa
select distinct name, id from A
执行后结果以下:code
其实是根据name和id两个字段来去重的,这种方式mySQL、Access和SQL Server同时支持。字符串
select distinct xing, ming from B
返回以下结果:select
返回的结果为两行,这说明distinct并不是是对xing和ming两列“字符串拼接”后再去重的,而是分别做用于了xing和ming列。im
select count(distinct name) from A; --表中name去重后的数目, mySQL和SQL Server支持,而Access不支持
count是不能统计多个字段的,下面的SQL在SQL Server和Access中都没法运行,在mysql中能够运行。统计
select count(distinct name, id) from A;
若想使用,请使用嵌套查询,以下:查询
select count(*) from (select distinct xing, name from B) AS M;
select id, distinct name from A; --会提示错误,由于distinct必须放在开头