日期:2014-05-18  浏览次数:20813 次

Sql语句distinct怎么用
select distinct(pclassname),id from Iproducts where pclassid between 21 and 28
查询pclassname的时候distinct起作用,但是加上ID就没有去除重复的。
那位大神帮我解决一下。

------解决方案--------------------
你 id 难道不是主键,有重复的?
------解决方案--------------------
select distinct pclassname,id from Iproducts where pclassid between 21 and 28
或者
select pclassname,id from Iproducts where pclassid between 21 and 28 group by pclassname,id
------解决方案--------------------
你这样写当然没去重复啊

SQL code
select *, count(distinct pclassname) Iproducts where pclassid between 21 and 28 group by name

------解决方案--------------------
写法有问题
------解决方案--------------------
ID是主键的话,一放上去就没法去重复了。多个ID对应一个pclassname的话,你只能显示其中一个ID而不是全部显示,否则一行也变为多行了。
------解决方案--------------------
C# code
 select distinct pclassname,count(id)  Iproducts where pclassid between 21 and 28 group by name