任何人都可以看到为什么第二个结果集产生0结果的任何原因,即使我独立运行查询第二个将返回结果??
SELECT GROUP_CONCAT(DISTINCT(mu.ID)) AS users
FROM `companies` c
JOIN `companysites` cs ON c.ID = cs.companyID
JOIN `users` mu ON cs.ID = mu.siteID
JOIN `targetcategories` tcat ON c.smTargetCategory = tcat.ID
WHERE isVendor = 0;
SET @in = "users";
SELECT *
FROM privileges
WHERE userID IN (@in);
我实际上需要将第二个查询作为删除查询,但希望在我执行之前检查结果。
答案 0 :(得分:0)
在子查询中传递您的第一个查询,以进行删除:
DELETE FROM privileges
WHERE userID IN (
SELECT GROUP_CONCAT(DISTINCT(mu.ID)) AS userID
FROM `companies` c
JOIN `companysites` cs ON c.ID = cs.companyID
JOIN `users` mu ON cs.ID = mu.siteID
JOIN `targetcategories` tcat ON c.smTargetCategory = tcat.ID
WHERE isVendor = 0
);
如果mysql返回错误,您可以尝试“封装”子查询:
DELETE p.*
FROM privileges p
WHERE userID IN (
SELECT userID
FROM
(SELECT GROUP_CONCAT(DISTINCT(mu.ID)) AS userID
FROM `companies` c
JOIN `companysites` cs ON c.ID = cs.companyID
JOIN `users` mu ON cs.ID = mu.siteID
JOIN `targetcategories` tcat ON c.smTargetCategory = tcat.ID
WHERE isVendor = 0) x);