我想在mysql中使用IN子查询,但我需要GROUP_CONCAT
才能拥有having
(我的子查询工作得很好。)
SELECT pd.* FROM Product pd
WHERE perimeter_id IN (SELECT GROUP_CONCAT(p.id)
FROM Perimeter p
GROUP BY p.network_id, p.productRange_id, p.country_id
HAVING count(id) > 1);
答案 0 :(得分:1)
您不必使用GROUP_CONCAT。此查询将返回具有多个ID的所有(network_id,productRange_id,country_id):
SELECT p.network_id, p.productRange_id, p.country_id, GROUP_CONCAT(p.id)
FROM Perimeter p
GROUP BY p.network_id, p.productRange_id, p.country_id
HAVING count(id) > 1
此查询将返回不同行中的所有ID:
SELECT p.ID
FROM
Perimeter p INNER JOIN (
SELECT p.network_id, p.productRange_id, p.country_id, GROUP_CONCAT(p.id)
FROM Perimeter p
GROUP BY p.network_id, p.productRange_id, p.country_id
HAVING count(id) > 1
) d ON (p.network_id, p.productRange_id, p.country_id) = (d.network_id, d.productRange_id, d.country_id)
这是你的最终查询:
SELECT pd.*
FROM Product pd
WHERE
perimeter_id IN (
SELECT p.ID
FROM
Perimeter p INNER JOIN (
SELECT p.network_id, p.productRange_id, p.country_id, GROUP_CONCAT(p.id)
FROM Perimeter p
GROUP BY p.network_id, p.productRange_id, p.country_id
HAVING count(id) > 1
) d ON (p.network_id, p.productRange_id, p.country_id) =
(d.network_id, d.productRange_id, d.country_id)
)
答案 1 :(得分:0)
我认为你可以做INNER JOIN
喜欢
SELECT pd.*
FROM Product pd
INNER JOIN Perimeter p ON pd.perimeter_id = p.id
GROUP BY p.network_id, p.productRange_id, p.country_id
HAVING count(DISTINCT p.id) > 1;