集合中的第二个查询未返回结果

时间:2015-08-06 13:12:08

标签: mysql

任何人都可以看到为什么第二个结果集产生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);

我实际上需要将第二个查询作为删除查询,但希望在我执行之前检查结果。

1 个答案:

答案 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);