SQL语句distinct的多个字段去重问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AinUser/article/details/82687916

经典例子

select distinct name, id from table

或者

select name,id from table group by name

像这样是错误的写法,distinct不起作用的

曲线救国写法:

select name,id from table  where id in (

      select min(id) from table group by name

)

思想:将需要去重的字段添加到where条件中,取出唯一id

          然后就可以获得去重之后的两个字段了

          不过我这边是你需要去重一个字段,展示两个字段的情况

还可以看如下文章

https://blog.csdn.net/yz357823669/article/details/78794050

https://blog.csdn.net/djun100/article/details/10452165

猜你喜欢

转载自blog.csdn.net/AinUser/article/details/82687916