[英]Two separate joins on the same query
I have a query to find duplicates in a table: 我有一个查询来查找表中的重复项:
SELECT sofferenze.id_soff, sofferenze.Descrizione
FROM sofferenze
INNER JOIN (
SELECT Descrizione
FROM sofferenze
GROUP BY Descrizione
HAVING count( id ) >1
)dup ON sofferenze.Descrizione = dup.Descrizione
ORDER BY Descrizione ASC
It works like a charm and gives me all the duplicated rows. 它像一个咒语一样工作,并给了我所有重复的行。 I also have another query that starting from sofferenze.id_soff will give me another value in another table: 我还有另一个查询,它从sofferenze.id_soff开始会在另一个表中给我另一个值:
SELECT cod_fisc
FROM anagrafiche
JOIN `rischiatura` ON anagrafiche.id_ndg = rischiatura.id_ndg
WHERE id_ogg = 'SF000000012'
AND id_ruolo = 'RU010000002'
Actually this second query is run for each row returned by the first query replacing in this line WHERE id_ogg='SF000000012'
the value 'SF000000012' with the value sofferenze.id_soff that is returned by the first query. 实际上该第二查询运行用于通过所述第一查询在这一行替换返回的每一行WHERE id_ogg='SF000000012'
与sofferenze.id_soff该值的值“SF000000012”由第一查询返回。
This code is not efficient because it runs several times the second query. 该代码效率不高,因为它运行第二次查询几次。 Are there any option that I can merge the two queries? 是否可以合并两个查询?
Why not combine it into a sub-query? 为什么不将其组合为子查询?
SELECT cod_fisc
FROM anagrafiche
JOIN `rischiatura` ON anagrafiche.id_ndg = rischiatura.id_ndg
WHERE id_ogg IN (SELECT x.id_soff
FROM sofferenze x
WHERE x.Descrizione IN (
SELECT ix.Descrizione
FROM sofferenze ix
GROUP BY ix.Descrizione
HAVING count( * ) >1
))
AND id_ruolo = 'RU010000002'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.