mySQL:如何从列表中的表中选择而不是在另一个列表中

发布于 2024-10-18 01:56:38 字数 429 浏览 0 评论 0原文

是否可以从表中选择值,这些值不存在于一个列表中,但确实存在于另一个列表中......或者反之亦然?

例如

SELECT COUNT(g.`property`) as `number`, g.`property` 
  FROM `foo` g 
 WHERE `theID` IS IN (SELECT `theID` 
                        FROM `tableofIDS` 
                       WHERE `theID` = '54252') 
          AND NOT IN (SELECT `theID` 
                        FROM `anotherTableofIDS` 
                       WHERE `theID` = '54252')

Is it possible to select values from a table, where they don't exist in one list, but do exist in another... or they other way around?

E.g.

SELECT COUNT(g.`property`) as `number`, g.`property` 
  FROM `foo` g 
 WHERE `theID` IS IN (SELECT `theID` 
                        FROM `tableofIDS` 
                       WHERE `theID` = '54252') 
          AND NOT IN (SELECT `theID` 
                        FROM `anotherTableofIDS` 
                       WHERE `theID` = '54252')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

去了角落 2024-10-25 01:56:38
SELECT COUNT(g.`property`) as `number`, g.`property`
FROM `foo` g
WHERE `theID` IN (SELECT `theID` FROM `tableofIDS` WHERE `theID` = '54252')
  AND `theID` NOT IN (SELECT `theID` FROM `anotherTableofIDS` WHERE `theID` = '54252')
GROUP BY g.`property`

或者,您可以使用性能更好的联接:

SELECT COUNT(g.`property`) as `number`, g.`property`
FROM `foo` g JOIN (
    SELECT `theID`
    FROM `tableofIDS`
    WHERE `theID` = '54252'
   ) id1 ON g.theID = id1.theID
  LEFT JOIN (
    SELECT `theID`
    FROM `anotherTableofIDS`
    WHERE `theID` = '54252'
  ) id2 ON g.theID = id2.theID
WHERE id2.theID IS NULL
GROUP BY g.`property`
SELECT COUNT(g.`property`) as `number`, g.`property`
FROM `foo` g
WHERE `theID` IN (SELECT `theID` FROM `tableofIDS` WHERE `theID` = '54252')
  AND `theID` NOT IN (SELECT `theID` FROM `anotherTableofIDS` WHERE `theID` = '54252')
GROUP BY g.`property`

Alternativly, you can use joins which will perform better:

SELECT COUNT(g.`property`) as `number`, g.`property`
FROM `foo` g JOIN (
    SELECT `theID`
    FROM `tableofIDS`
    WHERE `theID` = '54252'
   ) id1 ON g.theID = id1.theID
  LEFT JOIN (
    SELECT `theID`
    FROM `anotherTableofIDS`
    WHERE `theID` = '54252'
  ) id2 ON g.theID = id2.theID
WHERE id2.theID IS NULL
GROUP BY g.`property`
淡淡離愁欲言轉身 2024-10-25 01:56:38

没有仔细思考,但发现了两个语法错误。尝试

SELECT COUNT(g.`property`) as `number`, g.`property` FROM `foo` g WHERE `theID` IN (SELECT `theID` FROM `tableofIDS` WHERE `theID` = '54252') AND `theID` NOT EXISTS (SELECT `theID` FROM `anotherTableofIDS` WHERE `theID` = '54252')

编辑:将 NOT IN 更改为 NOT EXISTS

Didn't think it through but discovered 2 syntax errors. Try

SELECT COUNT(g.`property`) as `number`, g.`property` FROM `foo` g WHERE `theID` IN (SELECT `theID` FROM `tableofIDS` WHERE `theID` = '54252') AND `theID` NOT EXISTS (SELECT `theID` FROM `anotherTableofIDS` WHERE `theID` = '54252')

Edit: Changed NOT IN to NOT EXISTS

于我来说 2024-10-25 01:56:38

我想也许你正在寻找的是这样的:

    SELECT COUNT(g.property) as `number`,
           g.property
      FROM foo g
      JOIN tableofIDs i USING (theID)
RIGHT JOIN anothertableofIDs x USING (theID)
     WHERE g.theID = '54252'
       AND x.theID IS NULL
  GROUP BY g.property

I think maybe what you're looking for is just this:

    SELECT COUNT(g.property) as `number`,
           g.property
      FROM foo g
      JOIN tableofIDs i USING (theID)
RIGHT JOIN anothertableofIDs x USING (theID)
     WHERE g.theID = '54252'
       AND x.theID IS NULL
  GROUP BY g.property
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文