产品库 
--------------------------- 
id  cp_id  cp_name 
1  1001  水果 
2  1002  桌子 
3  1003  汽车 
4  1004  手机 
5  1005  药品 
6  1006  电脑 
7  1007  空调 
用户  
--------------------------- 
id  username  cp_id  times 
1    u5        1001    
2    u1        1005 
3    u5        1003 
4    u2        1004 
5    u3        1005 
6    u4        1007  
7    u5        1002 
8    u5        1002 
9    u8        1002 
10   u5        1001 查询username 为u5 的用户..有哪些产品  如果有多个一样的产品,只显示一个.并且 用户 表中的 cp_id 只显示不重复的       u5        1001      水果 
      u5        1003      汽车 
      u5        1002      桌子 

解决方案 »

  1.   

    select a.username,a.cp_id,b.cp_name from (select distinct username,cp_id from yh) a inner join cp b on a.cp_id=b.cp_id
      

  2.   

    --> 测试数据: [产品库]
    if object_id('[产品库]') is not null drop table [产品库]
    create table [产品库] (id int,cp_id int,cp_name varchar(4))
    insert into [产品库]
    select 1,1001,'水果' union all
    select 2,1002,'桌子' union all
    select 3,1003,'汽车' union all
    select 4,1004,'手机' union all
    select 5,1005,'药品' union all
    select 6,1006,'电脑' union all
    select 7,1007,'空调'--> 测试数据: [用户]
    if object_id('[用户]') is not null drop table [用户]
    create table [用户] (id int,username varchar(2),cp_id int,times sql_variant)
    insert into [用户]
    select 1,'u5',1001,null union all
    select 2,'u1',1005,null union all
    select 3,'u5',1003,null union all
    select 4,'u2',1004,null union all
    select 5,'u3',1005,null union all
    select 6,'u4',1007,null union all
    select 7,'u5',1002,null union all
    select 8,'u5',1002,null union all
    select 9,'u8',1002,null union all
    select 10,'u5',1001,nullselect distinct a.username,a.cp_id,
    (select cp_name from [产品库] where cp_id=a.cp_id) as cp_name 
    from [用户] as a where a.username='u5'
    /*
    ----------------
    u5 1001 水果
    u5 1002 桌子
    u5 1003 汽车
    */
      

  3.   

    create table #cp (id int,cp_id int, cp_name nvarchar(5))
    insert into #cp select 1,  1001,  '水果'
    union all select 2, 1002, '桌子'
    union all select 3,  1003,  '汽车' 
    union all select 4,  1004,  '手机' 
    union all select 5 , 1005,  '药品' 
    union all select 6,  1006,  '电脑' 
    union all select 7,  1007 , '空调'create table #yh (id int,username varchar(5),cp_id int)
    insert into #yh select 1,'u5',1001
    insert into #yh select 2,'u1',1005 
    insert into #yh select 3,'u5',1003 
    insert into #yh select 4,'u2',1004 
    insert into #yh select 5,'u3',1005 
    insert into #yh select 6,'u4',1007  
    insert into #yh select 7,'u5',1002 
    insert into #yh select 8,'u5',1002 
    insert into #yh select 9,'u8',1002 
    insert into #yh select 10,'u5',1001 
    goselect a.username,a.cp_id,b.cp_name from (select distinct username,cp_id from #yh) a inner join #cp b on a.cp_id=b.cp_id
    where a.username='u5'/*
    username cp_id       cp_name
    -------- ----------- -------
    u5       1001        水果
    u5       1002        桌子
    u5       1003        汽车(3 行受影响)
    */
      

  4.   

    --> 测试数据: [产品库]
    if object_id('[产品库]') is not null drop table [产品库]
    create table [产品库] (id int,cp_id int,cp_name varchar(4))
    insert into [产品库]
    select 1,1001,'水果' union all
    select 2,1002,'桌子' union all
    select 3,1003,'汽车' union all
    select 4,1004,'手机' union all
    select 5,1005,'药品' union all
    select 6,1006,'电脑' union all
    select 7,1007,'空调'--> 测试数据: [用户]
    if object_id('[用户]') is not null drop table [用户]
    create table [用户] (id int,username varchar(2),cp_id int,times sql_variant)
    insert into [用户]
    select 1,'u5',1001,null union all
    select 2,'u1',1005,null union all
    select 3,'u5',1003,null union all
    select 4,'u2',1004,null union all
    select 5,'u3',1005,null union all
    select 6,'u4',1007,null union all
    select 7,'u5',1002,null union all
    select 8,'u5',1002,null union all
    select 9,'u8',1002,null union all
    select 10,'u5',1001,nullgo
    select distinct  a.username,a.cp_id,b.cp_name
    from 用户 a 
    left join 产品库 b on a.cp_id = b.cp_id
    where a.username = 'u5'/*
    username cp_id       cp_name 
    -------- ----------- ------- 
    u5       1001        水果
    u5       1002        桌子
    u5       1003        汽车(所影响的行数为 3 行)
    */
      

  5.   

    distinct 和group by 有什么区别了.都是显示不重复的记录
      

  6.   

    select distinct username,u.cp_id,cp_name from usertable u
    jion pro on pro.cp_id=u.cp_id where username=5