有需求表和任务表两张表  
一个需求可以创建多个任务 一对多
需求表主键 X_ID
任务表有个字段:任务状态,值是(0:未完成 1:完成),当然也有外键X_ID
现在要把任务都完成的需求查出来,就是该需求下的任务的状态都是1,怎么写?再说清楚点 是该需求下所有任务都要完成,都是1才查出来

解决方案 »

  1.   

    任务表:task
    select distinct A.X_ID from task A where not exist(select 1 from task B where A.X_ID = B.X_ID and B.state = 0)
      

  2.   

    select * from 需求表 where X_ID in(select distinct a.X_ID from 任务表 a where a.X_ID not in(select b.X_ID from 任务表 b where a.X_ID = b.X_ID and b.任务状态 = 0) 
    and a.X_ID in(select c.X_ID from 任务表 c where  c.任务状态 = 1))
      

  3.   

    假设  需求表request(主键 X_ID)  任务表task(外键 X_ID,状态 status)select request.* from request  left join task on request.X_ID=task.X_ID where  task.X_ID not  in (select 1 from task where status='0')
      

  4.   

    需求表a
    任务表b
    select * from a, b where a.x_id=b.x_id and b.zt=1
      

  5.   

    select name from xuqiu xx right join (select distinct xid from renwu where xid not in (select xid from renwu where status=0)) cc on xx.id=cc.xid
      

  6.   

    外联结啊!mysql跟oracle有区别
      

  7.   

    需求表:A
    任务表:B
    说明:先从任务表中查出“未完成”的任务的X_ID,然后再查询需求表,剔除从任务表中查出的结果,剩下的就是全部任务都完成的需求信息。
    select A.* from A where not exists (select distinct B.X_ID from B where B.status=0)
      

  8.   

    select * from b where (select count(1) from b where a.X_ID=b.X_ID and  任务状态=0)=(select count(1) from b where 任务状态=1)
      

  9.   

    select * 
    from b 
    where a.X_ID=b.X_ID and 
    (select count(1) from b where   任务状态=0)=(select count(1) from b where 任务状态=1)