如何根据左连接条件获取 mysql 值
how to get mysql values based on left join condition
工作table
1.id
2.status = 'active'
3.name
修复table
1.repair id
2.job_id
3.process = 'yes|no'
4.status = '1|2'
工作table
id name status
1 test active
2 check active
修复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
这里我需要显示数据,其中(进程!= 'yes' 和 repair_status != 2)按 job_id
分组
我需要查询后的结果
---------------------------------------------
job_id name( job.name ) status( job.status )
------------------------------------------------
2 check active
如果您想要 process = 'yes'
和 status = 2
没有修复的工作,您可以使用 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
)
为了获得您指定的结果,您的意思是 process
不是 yes
对于 任何 行对于 job_id
.然后至少一行有 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;
工作table
1.id
2.status = 'active'
3.name
修复table
1.repair id
2.job_id
3.process = 'yes|no'
4.status = '1|2'
工作table
id name status
1 test active
2 check active
修复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
这里我需要显示数据,其中(进程!= 'yes' 和 repair_status != 2)按 job_id
分组我需要查询后的结果
---------------------------------------------
job_id name( job.name ) status( job.status )
------------------------------------------------
2 check active
如果您想要 process = 'yes'
和 status = 2
没有修复的工作,您可以使用 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
)
为了获得您指定的结果,您的意思是 process
不是 yes
对于 任何 行对于 job_id
.然后至少一行有 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;