gpt4 book ai didi

mysql - Table1 Left Join Table2 Minus(加入数据)

转载 作者:行者123 更新时间:2023-11-29 01:38:59 25 4
gpt4 key购买 nike

表 1) m_conservationsetting

FacilityId   Unit   CategoryId
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
2 1 1
2 2 1

唯一键(FacilityId 单元 CategoryId)

表 2) l_maintelog

FacilityId   Unit  CategoryId  Status
1 1 1 0
1 1 2 1
1 1 3 0
1 2 1 0
2 1 1 0
2 2 1 0

结果:

FacilityId   Unit   CategoryId
1 2 2

Table1 需要与 Table2 保持连接,它应该忽略连接结果并仅显示 table1 数据作为结果。Table1 LeftJoin Table2 -(加入数据)用于以下查询。得到结果的条件是查表2中的记录status=0

SELECT cs.FacilityId,Cs.Unit,cs.CategoryId 
FROM m_conservationsetting cs
LEFT JOIN l_maintelog ml
ON cs.FacilityId=ml.FacilityId and cs.Unit=ml.Unit
WHERE ml.Status=0
GROUP BY cs.CategoryId

最佳答案

如果你只想获取那些不在left join结果中的记录,那么这样做:

SELECT t.* FROM m_conservationsetting AS t
WHERE NOT EXISTS (
SELECT cs.FacilityId,Cs.Unit,cs.CategoryId
FROM m_conservationsetting AS cs
LEFT JOIN l_maintelog ml on
(cs.FacilityId=ml.FacilityId and cs.Unit=ml.Unit)
WHERE ml.Status=0
group by cs.CategoryId
)

关于mysql - Table1 Left Join Table2 Minus(加入数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31217433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com