distinct left out join group by order by之去重

distinct 能去重复的记录 但它这去重是有限制的 要所有显示的字段完全相同才能去重

如果只需去重显示总数之类的可以这样

count(distinct id)

如果根据某个字段去重则结合order by 如

select name,id,password from user group by name;

这样就能把name相同的去掉 如果在这基础上排序的话 排序的字段要在group by 里 如

select name,id,password from user group by name,id  order by id;

left join

left join 总是跟着 on  以on为基准 左边有的显示 左面没有 又边有的不显示  如果左边单条符合条件右边多条符合条件 则显示多条 这样就有重复数据了 左边那个字段重复  如果想要去重的话 可以用distinct去重 不过这个限制有点大 如

select  name,age from student  as s left join (select distinct money from product ) as p on p.id=s.product_id;

这样的话product表money相同的记录就会被干掉 但是如果这是后还许要显示product的content的话 那content和name都相同才会不显示 明显不合要求

只能使用where语句让product显示符合条件的记录 

left join 和group by 结合去重

select name,age form student as s left join product as p on p.id=s.product_id group by a.name,money order by money;

根据name去重 根据money排序

猜你喜欢

转载自blog.csdn.net/nailsoul/article/details/52207931