简体   繁体   English

如何根据左连接条件获取mysql值

[英]how to get mysql values based on left join condition

job table工作表

1.id
2.status = 'active'
3.name

repair table维修台

1.repair id
2.job_id
3.process = 'yes|no'
4.status  = '1|2'

job table工作表

id   name  status
1    test  active
2    check active

repair table维修台

repair_id     job_id  process  status
1                1      no        2
2                1      no        1
3                1      yes       2
4                2      no        1
5                2      no        2

here i need to show data which ( process != 'yes' and repair_status != 2 ) group by job_id在这里,我需要显示按 job_id 分组的数据( process != 'yes' 和 repair_status != 2 )

i need result after query我需要查询后的结果

---------------------------------------------
job_id    name( job.name ) status( job.status )
------------------------------------------------
2            check           active

In order to get the results that you specify, you mean that process is not yes for any row for the job_id .为了获得您指定的结果,您的意思是该process对于job_id任何行都不是yes And then that at least one row has a status <> 2. That would be:然后至少一行的status <> 2. 那将是:

select j.job_id, j.name, j.status
from repair r join
     job j
     on r.job_id = r.id
group by j.job_id, j.name, j.status
having max(process) = 'no' and
       min(repair_status) = 1;

If you want jobs for which no repair exists with process = 'yes' and status = 2 , you can use not exists :如果您想要在process = 'yes'status = 2 not exists修复的作业,您可以使用not exists

select j.*
from jobs j
where not exists (
    select 1 
    from repair r 
    where r.job_id = j.id and r.process = 'yes' and r.status = 2
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM