美文网首页
sql distinct 不用去重的字段也需要显示

sql distinct 不用去重的字段也需要显示

作者: Sunnky | 来源:发表于2018-01-19 20:42 被阅读0次

table

id name
1 a
1 b
2 c

此时想要得到这种结果

id name
1 a
2 c

想到用DISTINCT 解决,但是会发现有个问题
select distinct name from table
结果如下:

id
1
2

虽然id是不重复了,但是也没有name信息,这时应该如下操作:
select *, count(distinct name) from table group by name
结果:
select id name count(distinct name) from table

1 a 1
2 c 1

最后一项是多余的,不用管就可以了,目的达到了

相关文章

网友评论

      本文标题:sql distinct 不用去重的字段也需要显示

      本文链接:https://www.haomeiwen.com/subject/mxpaoxtx.html