如果status = N,则需要resultset,如果status = N且status = P,则结果不显示

时间:2014-01-17 15:36:40

标签: mysql mysqli

table:master_consmnt:

fra_code    mode       cnum     cost    edate      status
ET7867FRA   SURFACE     19001   10.000  2014-01-17  P
ET7867FRA   SURFACE     19005   5.000   2014-01-17  P
ET7867FRA   SURFACE     19005   10.000  2014-01-17  P
FRANE3981   SURFACE     19005   0.000   2014-01-17  P
FRARE5664   SURFACE     19005   18.000  2014-01-17  N
FRARE5664   SURFACE     19001   14.000  2014-01-17  N
FRARE5664   SURFACE     180001  38.000  2014-01-17  N

我想得到以下结果,请仔细查看master_consmnt表,该表有多个cnum,状态= P或N.我们想要一个结果集,如果没有status =“P”且状态=“N,则有cnum ”

fra_code    mode      cnum  cost    edate           status
FRARE5664   SURFACE 180001  38.000  2014-01-17  N

1 个答案:

答案 0 :(得分:1)

您可以使用not exists

执行此操作
select mc.*
from master_consmnt mc
where mc.status = 'N' and
      not exists (select 1
                  from master_consmnt mc2
                  where mc2.cnum = mc.cnum and status = 'P'
                 );