有两张表
A表:ID  姓名 
     1   张XX
     2   李XX
     ..  ..
B表:ID  疾病
     1   糖尿病
     1   高血压
     2   高血压
     ..  ....A表是病人的基本信息,B表是病人患的疾病,有的人可能会得多种疾病。现在需要查询:1。既得糖尿病又得高血压的人
              2。得糖尿病却没有得高血压的人
              3。既没有得糖尿病又没有得高血压的人
SQL语句怎么写?

解决方案 »

  1.   

    1.
    Select A.*
    From A 
    Inner Join B On A.ID = B.ID
    Inner Join B C On A.ID = C.ID
    Where B.疾病 = '糖尿病' And C.疾病 = '糖尿病'
      

  2.   

    --上面錯了點,改下
    --1.
    Select A.*
    From A 
    Inner Join B On A.ID = B.ID
    Inner Join B C On A.ID = C.ID
    Where B.疾病 = '糖尿病' And C.疾病 = '高血压'
    --2.
    Select A.*
    From A 
    Inner Join B On A.ID = B.ID
    Where B.疾病 = '糖尿病'
    And Not Exists(Select ID From B Where ID = A.ID And 疾病 = '高血压')
      

  3.   

    --3.
    Select A.* From A 
    Where Not Exists(Select ID From B Where ID = A.ID And 疾病 = '糖尿病')
    And Not Exists(Select ID From B Where ID = A.ID And 疾病 = '高血压')
      

  4.   

    3 Select A.*
    From A
    Inner Join B On A.ID = B.ID
    where b.疾病 not in('糖尿病','高血压')
      

  5.   


    Create Table A
    (ID Int,
     姓名 Nvarchar(20))
    Insert A Select 1,   N'张XX'
    Union All Select     2,   N'李XX'
    Union All Select     3,   N'王XX'Create Table B
    (ID Int,
     疾病 Nvarchar(20))
    Insert B Select 1,   N'糖尿病'
    Union All Select     1,   N'高血压'
    Union All Select     2,   N'糖尿病'
    Union All Select     3,   N'感冒'
    GO
    --1。既得糖尿病又得高血压的人
    Select A.*
    From A 
    Inner Join B On A.ID = B.ID
    Inner Join B C On A.ID = C.ID
    Where B.疾病 = N'糖尿病' And C.疾病 = N'高血压'
    -- 2。得糖尿病却没有得高血压的人
    Select A.*
    From A 
    Inner Join B On A.ID = B.ID
    Where B.疾病 = N'糖尿病'
    And Not Exists(Select ID From B Where ID = A.ID And 疾病 = N'高血压')--3。既没有得糖尿病又没有得高血压的人
    Select A.* From A 
    Where Not Exists(Select ID From B Where ID = A.ID And 疾病 = N'糖尿病')
    And Not Exists(Select ID From B Where ID = A.ID And 疾病 = N'高血压')
    GO
    Drop Table A, B
    --Result
    /*
    --1結果
    ID 姓名
    1 张XX--2結果
    2 李XX--3結果
    3 王XX
    */
      

  6.   

    gbys(站的高,不一定就尿的远) ( ) 信誉:100    Blog   加为好友  2007-07-02 10:08:54  得分: 0  
     
     
       3 Select A.*
    From A
    Inner Join B On A.ID = B.ID
    where b.疾病 not in('糖尿病','高血压')
      
     
    ----------
    這個不正確,得到的是“沒得糖尿病或者沒得高血压的人”.
      

  7.   

    if object_id('a')>0
     drop table a
    if object_id('b') >0
    drop table b 
    create table a (id int,name varchar(20))
    insert into a
    select 1,'張XX'
    union all
    select 2,'李XX'create table b(id int,jibin varchar(20))
    insert into b
    select      1,' 糖尿病'
    union all 
     select     1,   '高血壓'
    union all 
    select     2,  ' 高血壓'goalter function fun_str(@id int)
    returns varchar(200)
    begin 
    declare @str varchar(200),@exec varchar(8000)
    set @str=''
    select @str=@str+','+jibin from b where id=@id
    set @str=stuff(@str,1,1,'')
    return (@str)
    endgoselect *,dbo.fun_str(id) as jibin  from a/*
    id                     name        jibin
    ------------------------------------------------------
    1 張XX  糖尿病,高血壓
    2 李XX  高血壓
    */
      

  8.   

    if object_id('a')>0
     drop table a
    if object_id('b') >0
    drop table b 
    create table a (id int,name varchar(20))
    insert into a
    select 1,'張XX'
    union all
    select 2,'李XX'create table b(id int,jibin varchar(20))
    insert into b
    select      1,' 糖尿病'
    union all 
     select     1,   '高血壓'
    union all 
    select     2,  ' 高血壓'goalter function fun_str(@id int)
    returns varchar(200)
    begin 
    declare @str varchar(200)
    set @str=''
    select @str=@str+','+jibin from b where id=@id
    set @str=stuff(@str,1,1,'')
    return (@str)
    endgo select id,name,
    case   when charindex('糖尿病',dbo.fun_str(id))>0 
              and  charindex('高血壓',dbo.fun_str(id))>0
           then '既得糖尿病又得高血壓的人'
           when charindex('糖尿病',dbo.fun_str(id))>0
              and  charindex('高血壓',dbo.fun_str(id))=0
             then '得糖尿病沒有得高血壓的人'
            when  charindex('糖尿病',dbo.fun_str(id))=0
              and  charindex('高血壓',dbo.fun_str(id))>0
             then '得糖尿病沒有得高血壓的人'
              end  as jibin
    from a
    /*
    id                  name                  jibin
    --------------------------------------------------------------
    1 張XX 既得糖尿病又得高血壓的人
    2 李XX 得糖尿病沒有得高血壓的人
    */
        
      

  9.   

    select 姓名from A where id in(select 疾病from B where 疾病 in ('糖尿病','高血压'))
    select 姓名from A where id in(select 疾病from B where 疾病 in ('糖尿病') and 疾病 <> '高血压')
    select 姓名from A where id in(select 疾病from B where 疾病 not in ('糖尿病','高血压'))