关于Sql查询

时间:2017-05-30 13:24:30

标签: mysql

AHLBLOCKNO  AHLSLNOHHD  SLNOMEMBER  NAME        GENDERID
0193        0073        001         BABLA      MALE
0193        0073        002         VINAYBHAI   MALE
0193        0073        004         SANJANABEN FEMALE
0193        0073        005         VISHALBHAI  MALE
0193        0105        001         ASHVINBHAI  MALE
0193        0105        003         PARESHBHAI  MALE
0193        0105        002         ARUNABEN    FEMALE
0193        0143        001         CHHAKABHAI  MALE
0193        0143        003         FAKIRBHAI   MALE
0193        0143        004         SUMITRABEN  FEMALE
0193        0143        005         PANKAJBHAI  MALE
0193        0143        002         VANITABEN   FEMALE
0193        0143        007         PRIYANSHAN  FEMALE
0193        0162        001        RAMANBHAI    MALE
0193        0162        003        KAVITABEN    FEMALE
0193        0162        005         SANJAYBHAI  MALE
0193        0162        004        SUNILBHAI    MALE
0193        0162        002        BIPINBHAI    MALE
0193        9999        001        bhimjibhai   MALE

在上表中,我想得到AHLSLNOHHD这样的{%}},其中没有一个性别等于女性。

预期输出为:

AHLBLOCKNO  AHLSLNOHHD  SLNOMEMBER  NAME        GENDERID
0193        9999        001        bhimjibhai   MALE

2 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

# SELECT all columns from your table  
SELECT t1.* FROM tablename t1 
   # LEFT OUTER JOIN to same table 
   LEFT JOIN tablename t2 ON 
      # WITH SAME AHLSLNOHHD
      t1.AHLSLNOHHD = t2.AHLSLNOHHD 
      # AND where GENDERID is FEMALE
      AND t2.GENDERID = 'FEMALE'
 # NOW only consider rows where no such join (with female) could be found:
 WHERE t2.AHLSLNOHHD IS NULL

答案 1 :(得分:0)

SELECT * FROM Table1 WHERE Table1.AHLBLOCKNO  NOT IN (SELECT AHLBLOCKNO FROM Table1 where GENDERID = 'FEMALE')