有一表产品表(product)如下:id    name    code 
1     a       101
2     a       101
3     a       101
4     x       101
5     b       103
6     b       103
7     z       103
8     c       106
9     m       106
10    c       106要求把同code 不同name 记录列出下结果
name  code
a     101
x     101
b     103
z     103
c     106
m     106

解决方案 »

  1.   


    select distinct name,code from product;
    --或
    select name,code from product group by name,code;
      

  2.   

    select distinct a.name ,a.code
    from pyk a,pyk b
    where a.code=b.code and a.name<>b.name
    order by a.code
      

  3.   

    1.有group by 的地方不要有聚合函数。
    2.数据库优化中明确提过  尽量不用distinct 最好用group by 替代所以根据你的数据 可以这样:select name,code,avg(code) from product group by name,code;
      

  4.   

    好像比较简单,不知道我的理解有没有错,sql语句如下:
    select unique a.name,a.code from product a,product b where a.name<>b.name and a.code=b.code;
      

  5.   

    select distinct a.name,a.code from product a where a.code in (select distinct b.code from product b)