[英]Mix two sql queries: return what's requested in the first one but ignoring the second one
I have two queries which work properly: 我有两个可以正常工作的查询:
SELECT DISTINCT MarcadaProduccio.Article,
SUM(MarcadaProduccio.Rendiment * MarcadaProduccio.HoresMarcada)/SUM(MarcadaProduccio.HoresMarcada) AS MitjanaPonderada
FROM MarcadaProduccio
INNER JOIN MarcadaProduccio ON ParosMarcadesProduccio.IDMarcada = MarcadaProduccio.ID
WHERE MarcadaProduccio.Article IN (SELECT DISTINCT MarcadaProduccio.Article FROM MarcadaProduccio)
GROUP BY MarcadaProduccio.Article
SELECT ParosMarcadesProduccio.IDMarcada
FROM ParosMarcadesProduccio
WHERE ParosMarcadesProduccio.DescripcioParo LIKE "%MONTAJE%"
What I would like is to do one query which SELECTs as requested in the first query but ignoring the rows returned by the second query when ParosMarcadesProduccio.IDMarcada = MarcadaProduccio.ID. 我想做的是一个查询,该查询按第一个查询的要求进行选择,但是当ParosMarcadesProduccio.IDMarcada = MarcadaProduccio.ID时,忽略第二个查询返回的行。
How do I do that? 我怎么做?
Thank you 谢谢
PD: Structure of the tables MarcadaProduccio and ParosMarcadesProduccio PD:表的结构MarcadaProduccio和ParosMarcadesProduccio
Your first query cannot work, because ParosMarcadaProduccio
is not defined. 您的第一个查询无法工作,因为
ParosMarcadaProduccio
。 You can simplify the query to something like this: 您可以将查询简化为以下形式:
SELECT mp.Article, SUM(mp.Rendiment * mp.HoresMarcada) / SUM(mp.HoresMarcada) AS MitjanaPonderada
FROM MarcadaProduccio mp INNER JOIN
ParosMarcadaProduccio pmp
ON pmp.IDMarcada = mp.ID
WHERE mp.Article IN (SELECT mp2.Article FROM MarcadaProduccio mp2)
GROUP BY mp.Article;
Note that the SELECT DISTINCT
s are not needed. 请注意,不需要
SELECT DISTINCT
。
SELECT mp.Article, SUM(mp.Rendiment * mp.HoresMarcada) / SUM(mp.HoresMarcada) AS MitjanaPonderada
FROM MarcadaProduccio mp INNER JOIN
ParosMarcadaProduccio pmp
ON pmp.IDMarcada = mp.ID
WHERE mp.Article IN (SELECT mp2.Article FROM MarcadaProduccio mp2) AND
NOT EXISTS (SELECT pmp.IDMarcada
FROM ParosMarcadaProduccio pmp2
WHERE pmp2.DescripcioParo LIKE '%MONTAJE%' AND
pmp.IDMarcada = mp.id
)
GROUP BY mp.Article;
When learning to use subqueries for filtering, I strongly recommend NOT EXISTS
over NOT IN
. 在学习使用子查询进行过滤时,强烈建议
NOT EXISTS
取代NOT IN
。 The latter behaves strangely when the subquery returns NULL
values. 当子查询返回
NULL
值时,后者的行为很奇怪。 You might as well learn the better way first. 您最好先学习更好的方法。
Use the following approach using NOT IN
, just make sure that the ParosMarcadesProduccio.IDMarcada
is never NULL
. 使用
NOT IN
使用以下方法,只需确保ParosMarcadesProduccio.IDMarcada
永远不会为NULL
。
SELECT MarcadaProduccio.ID, MarcadaProduccio.Article,
SUM(MarcadaProduccio.Rendiment * MarcadaProduccio.HoresMarcada)/SUM(MarcadaProduccio.HoresMarcada) AS MitjanaPonderada
FROM MarcadaProduccio
INNER JOIN MarcadaProduccio ON ParosMarcadesProduccio.IDMarcada = MarcadaProduccio.ID
WHERE MarcadaProduccio.Article IN (SELECT DISTINCT MarcadaProduccio.Article FROM MarcadaProduccio) and
MarcadaProduccio.ID NOT IN (
--your second query
SELECT ParosMarcadesProduccio.IDMarcada
FROM ParosMarcadesProduccio
WHERE ParosMarcadesProduccio.DescripcioParo LIKE "%MONTAJE%"
)
GROUP BY MarcadaProduccio.ID, MarcadaProduccio.Article
EDIT: as noted by others, DISTINCT
seems suspicious in your query and it should be probably removed. 编辑:正如其他人所指出的,
DISTINCT
在您的查询中似乎可疑,应该将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.