一、distinct與null的關系見鏈接
https://www.cnblogs.com/jasonlam/p/6619013.html
distinct show 的時候不會出現null或none
二、distinct調優
use test; create table mytest(id int,name varchar(20)) insert into mytest values(1,'zs'),(2,'zs'),(3,'zs'),(4,'yan');
distinct適合單列使用:
select distinct name from users
以下寫法直接拿兩列聯合去重:
select distinct id,name from users
以下寫法直接報錯:
select id,distinct name from users
=> 如何高效率的去重單列,又保留其他字段?
# id最大之前的都返回(這些是需要delete的)
select a.* from mytest a where exists(select 1 from mytest b where a.name = b.name and a.id < b.id )
# id最小之后的都返回
select a.* from mytest a where exists(select 1 from mytest b where a.name = b.name and a.id > b.id )
結論:
select a.* from mytest a where exists (select 1
from (select max(id) id,name from mytest group by name)b where a.name = b.name and a.id = b.id
)
## (不刪,直接select去重)數據量越大,性能越強
## 任意相同,而不是全部相同