以下为一用户兴趣记录表的示例数据,其中User_id为用户编号,Interest_Id为兴趣编号,求出与某用户兴趣完全一致的所有用户(如此示例数据中,与用户编号为1的用户的兴趣完全一致的用户为用户编号为3和8的两位用户),请写出相关的SQL语句(遵循SQL92标准,不得使用T-SQL,PL/SQL等扩展语言)。User_id Interest_Id
1           1
1           22           1
2           3
3           1
3           24           1
4           2
4           3
5           7
5           8
6           1
7           2
8           1
8           29           1
9           3
9           7
9           8
10          9
10          11

解决方案 »

  1.   

    SELECT * FROM TB T WHERE User_id
    IN(
        SELECT User_id FROM TB
        WHERE Interest_Id  IN(SELECT Interest_Id FROM TB WHERE  User_id=1) 
       GROUP BY USER_ID HAVING COUNT(1)=(SELECT COUNT(1) FROM TB WHERE  User_id=1)
    )
      

  2.   

    SELECT * FROM TB T WHERE User_id
    IN(
      SELECT distinct User_id FROM TB
      WHERE Interest_Id IN(SELECT Interest_Id FROM TB WHERE User_id=1)  
      GROUP BY USER_ID HAVING COUNT(1)=(SELECT COUNT(1) FROM TB WHERE User_id=1)
    )修改一下
      

  3.   

    select id from tb where Interest_Id=(select top 1 Interest_Id from tb where User_id=1) and User_id<>1
      

  4.   

    我插,被忽悠了
    select id from tb where Interest_Id=(select top 1 Interest_Id from tb where [User_id]=1) and [User_id]<>1
      

  5.   

    又被忽悠了select [User_id] from tb
    where Interest_Id=(select top 1 Interest_Id from tb where [User_id]=1)
    and [User_id]<>1
      

  6.   


    User_id Interest_Id
    1 1
    1 2
    2 1
    2 3
    3 1
    3 2
    4 1
    4 2
    4 3
    5 7
    5 8
    6 1
    7 2
    8 1
    8 2
    9 1
    9 3
    9 7
    9 8
    10 9
    10 11排版问题,不好意思!
      

  7.   


    楼上的所好像都有问题,我试了,他们把user_id为4的也统计进来了让我想想
      

  8.   

    1 22 1
    第二行中的最后这个1是你标注上去显示明显,还是里面有这个1值。
    同理,3号,和8号都有。
    如果有这个1值,我再写过题目有点不清晰,不知道这样理解的题目,写得对不对!create table abc
    (
    userid int,
    Interestid int
    );
    insert into abc values (1,1)
    insert into abc values (1,22)
    insert into abc values (2,3)
    insert into abc values (3,1)
    insert into abc values (3,24)
    insert into abc values (4,2)
    insert into abc values (4,3)
    insert into abc values (4,4)
    insert into abc values (4,5)
    insert into abc values (5,8)
    insert into abc values (6,1)
    insert into abc values (7,4)
    insert into abc values (8,1)
    insert into abc values (8,29)
    insert into abc values (9,11)select userid from abc where Interestid in 
    (select count(Interestid)*10+userid+(select top 1 Interestid from abc) as row from abc group by userid)
    结果:
    /*
          userid
    ------------------------------------------
            1
    3
    8
    */
      

  9.   

    /*
    M
    as
    (
    select distinct a.*,
    case when a.zd1=b.zd1 then null else b.zd1 end as col1,
    case when a.zd2=b.zd2 then null else b.zd2 end as col2,
    case when a.zd1=b.zd1 and a.zd2=b.zd2 and a.zd3=b.zd3 then null else b.zd3 end as col3,
    case when a.zd1=b.zd1 and a.zd2=b.zd2 and a.zd4=b.zd4 then null else b.zd4 end as col4 
    from T a inner join T b on a.zd1=b.zd1
    )
    select *from M
    */
    select
      case when px=1 then zd1 else null end as zd1,
      case when px=1 then zd2 else null end as zd2,
      case when a.zd3=zd3 then zd3 else null end as zd3,
      case when a.zd4=zd4 then zd4 else null end as zd4
    from 
    (select px=row_number()over(partition by zd1 order by getdate()),* from T)a 
    inner join T on a.zd1=T.zd1/*
    以下为一用户兴趣记录表的示例数据,其中User_id为用户编号,Interest_Id为兴趣编号,
    求出与某用户兴趣完全一致的所有用户(如此示例数据中,与用户编号为1的用户的兴趣完
    全一致的用户为用户编号为3和8的两位用户),请写出相关的SQL语句
    (遵循SQL92标准,不得使用T-SQL,PL/SQL等扩展语言)。User_id Interest_Id
    1 1
    1 2
    2 1
    2 3
    3 1
    3 2
    4 1
    4 2
    4 3
    5 7
    5 8
    6 1
    7 2
    8 1
    8 2
    9 1
    9 3
    9 7
    9 8
    10 9
    10 11
    */
    if object_id('tbl')is not null
    drop table tblcreate table tbl(
    [user_id] int,
    Interest_Id int
    )insert tbl
    select 1,1 union all
    select 1,2 union all
    select 2,1 union all
    select 2,3 union all
    select 3,1 union all
    select 3,2 union all
    select 4,1 union all
    select 4,2 union all
    select 4,3 union all
    select 5,7 union all
    select 5,8 union all
    select 6,1 union all
    select 7,2 union all
    select 8,1 union all
    select 8,2 union all
    select 9,1 union all
    select 9,3 union all
    select 9,7 union all 
    select 9,8 union all
    select 10,9 union all
    select 10,11
    drop proc p_proc
    select *from tbl 
    go
    create proc p_proc @user_id int
    as
    declare @count int
    create table #t(
    num int,
    [user_id] int,
    Interest_Id int,
    [count] int
    )
    insert #t
    select ROW_NUMBER()OVER(partition by a.[user_id] order by getdate()) as num,
    a.*,b.[count] from tbl a
    inner join (select [user_id],count(*) as [count] from tbl group by [user_id])b
    on a.[user_id]=b.[user_id]
    select distinct @count=[count] from #t where [user_id]=@user_id
    select c.[user_id],count(c.Interest_Id) as InterestNum from
    (select distinct [user_id],Interest_Id from #t 
    where Interest_Id in (select Interest_Id from #t where [user_id]=@user_id) and [count]=@count
    and [user_id]<>@user_id)c group by c.[user_id] having count(c.Interest_Id)=@countexec p_proc 结果:
    user_id InterestNum
    3 2
    8 2这个能行么?不知道是不是巧合
      

  10.   


    declare @t table ([User_id] int,Interest_Id int)
    insert into @t
    select 1,1 union all select 1,2 union all select 2,1 union all
    select 2,3 union all select 3,1 union all select 3,2 union all
    select 4,1 union all select 4,2 union all select 4,3 union all
    select 5,7 union all select 5,8 union all select 6,1 union all
    select 7,2 union all select 8,1 union all select 8,2 union all
    select 9,1 union all select 9,3 union all select 9,7 union all
    select 9,8 union all select 10,9 union all select 10,11SELECT b.[User_id] FROM @t a,@t b
    WHERE a.[User_id] = 1 AND a.Interest_Id = b.Interest_Id
    GROUP BY b.[User_id]
    HAVING (COUNT(1) = (SELECT COUNT(1) FROM @t WHERE [User_id] = 1)
        AND COUNT(1) = (SELECT COUNT(1) FROM @t WHERE [User_id] = b.[User_id]))
    /*
    User_id
    -----------
    1
    3
    8
    */
      

  11.   

    DECLARE @T TABLE (User_id INT, Interest_Id INT)
    INSERT @T
    SELECT 1,  1 UNION ALL
    SELECT 1,  2 UNION ALL
    SELECT 2,  1 UNION ALL
    SELECT 2,  3 UNION ALL
    SELECT 3,  1 UNION ALL
    SELECT 3,  2 UNION ALL
    SELECT 4,  1 UNION ALL
    SELECT 4,  2 UNION ALL
    SELECT 4,  3 UNION ALL
    SELECT 5,  7 UNION ALL
    SELECT 5,  8 UNION ALL
    SELECT 6,  1 UNION ALL
    SELECT 7,  2 UNION ALL
    SELECT 8,  1 UNION ALL
    SELECT 8,  2 UNION ALL
    SELECT 9,  1 UNION ALL
    SELECT 9,  3 UNION ALL
    SELECT 9,  7 UNION ALL
    SELECT 9,  8 UNION ALL
    SELECT 10,  9 UNION ALL
    SELECT 10,  11SELECT * FROM @T T WHERE User_id
    IN(
      SELECT User_id FROM @T T1
      WHERE Interest_Id IN(SELECT Interest_Id FROM @T WHERE User_id=1)  
      GROUP BY USER_ID 
    )AND (SELECT COUNT(DISTINCT Interest_Id) FROM @T WHERE USER_ID=T.USER_ID  ) =(SELECT COUNT(1) FROM @T WHERE User_id=1)
    /*
    (所影响的行数为 21 行)User_id     Interest_Id 
    ----------- ----------- 
    1           1
    1           2
    2           1
    2           3
    3           1
    3           2
    8           1
    8           2(所影响的行数为 8 行)楼主自己修改一下不就可以了,如果要限制所有的都一样就放外面比较
      

  12.   


    SELECT * FROM @T T 
    WHERE EXISTS(SELECT 1 FROM @T WHERE Interest_Id=T.Interest_Id AND User_id=1)
    AND 
    (SELECT COUNT(Interest_Id) FROM @T WHERE USER_ID=T.USER_ID) =(SELECT COUNT(1) FROM @T WHERE User_id=1)再简化一点。
      

  13.   

    问题由于排版问题描述不清,请大家转看另一贴,此贴送分<a href="http://topic.csdn.net/u/20120219/16/f24e452e-1d65-47dc-9232-2412891a6182.html">http://topic.csdn.net/u/20120219/16/f24e452e-1d65-47dc-9232-2412891a6182.html</a>