UserID   UserName   Age   MID
102      小名       29    106
104      小王       28    106
106      小红       27    108
107      小时       25    
MID是UserName的主管的ID,请查出年龄比主管大的UserID,UserName
谢谢各位。

解决方案 »

  1.   

    trySelect 
    A.UserID,
    A.UserName
    From
    TableName A
    Inner Join
    TableName B
    On A.MID = B.UserID
    Where A.Age > IsNull(B.Age, 0)
      

  2.   

    Select * from 表 as t
    where exists(Select * from 表 where t.Mid=UserID and t.Age>Age)
      

  3.   

    要精通SQL语句有什么好书吗,推荐一下啊,各位朋友
      

  4.   

    create table #
    (
    UserID varchar(3),
    UserName varchar(10),
    Age int,
    MID varchar(3)
    )insert into #
    select '102'    ,  '小名'  ,     29  ,  '106'
    union all select '104'   ,   '小王'   ,    28  ,  '106'
    union all select '106'  ,    '小红'  ,     27  ,  '108'
    union all select '107'  ,    '小时'  ,     25 ,'104'
    select * from #select a.*,b.*
    from # a left join # b on (a.mid=b.userid)
    where a.age>b.age==================
    UserID,UserName,Age,MID
    102,小名,29,106
    104,小王,28,106
    106,小红,27,108
    107,小时,25,104(所影响的行数为 4 行)UserID,UserName,Age,MID,UserID,UserName,Age,MID
    104,小王,28,106,106,小红,27,108
    102,小名,29,106,106,小红,27,108(所影响的行数为 2 行)