使用 if 条件从两个表中进行选择的 SQL 查询

发布于 2024-12-21 19:27:47 字数 750 浏览 0 评论 0原文

我有两个表“通知”和“确认”。确认有一个字段保存通知表的主键。基本上一个通知会有很多确认。

Tables: Notification        Acknowledgment

fields: id, notifier        id, parent_id, status

现在我必须从通知中选择行,以便:

  1. 没有确认 WHERE Acknowledment.parent_id = notification.id (对于该特定通知基本上没有确认) //or
  2. if there is an Acknowledgment for notification, then select notification if any of the Acknowledgments withparent_id = notification.id has a Acknowledgment.status = someValue

伪 SQL 代码:

   "SELECT * FROM Notification (WHERE id is not present in Acknowledgment.parent_id) OR
   (WHERE id is present in Acknowledgment.parent_id AND Acknowledgment.status=@someValue"

我可以将其分解为更简单的查询并实现此目的,但我我很想知道一个查询来完成这项工作..

I have two tables Notification and Acknowledgment. Acknowledgment has a field which holds the primary key of Notification table. Basically a Notification will have many Acknowledgments.

Tables: Notification        Acknowledgment

fields: id, notifier        id, parent_id, status

Now I have to choose rows from Notification such that:

  1. there is no Acknowledgment WHERE Acknowledment.parent_id = Notification.id (basically no Acknowledgment for that particular Notification)
    //or
  2. if there is an Acknowledgment for Notification, then select Notification if any of the Acknowledgments with parent_id = Notification.id has a Acknowledgment.status = someValue

A pseudo SQL code:

   "SELECT * FROM Notification (WHERE id is not present in Acknowledgment.parent_id) OR
   (WHERE id is present in Acknowledgment.parent_id AND Acknowledgment.status=@someValue"

I can break it into simpler queries and achieve this, but I would love to know one single query to get this done..

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

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

发布评论

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

评论(3

今天小雨转甜 2024-12-28 19:27:47

作为LEFT OUTER JOINS的替代方案,您可以使用EXISTS 子句

对于您的示例:

SELECT *
FROM Notification n
WHERE NOT EXISTS (
   SELECT *
   FROM Acknowledgement
   WHERE parent_id = n.id
) OR EXISTS (
   SELECT *
   FROM Acknowledgement
   WHERE parent_id = n.id AND status = @someValue
)

JOIN 在 SQL 中往往会得到更好的优化(特别是在我的示例中,您在同一个表上使用多个连接),我只是提到这种替代方案,因为它是一种更直接地翻译您的伪查询。

As an alternative to LEFT OUTER JOINS, you can use the EXISTS clause.

For your example:

SELECT *
FROM Notification n
WHERE NOT EXISTS (
   SELECT *
   FROM Acknowledgement
   WHERE parent_id = n.id
) OR EXISTS (
   SELECT *
   FROM Acknowledgement
   WHERE parent_id = n.id AND status = @someValue
)

JOINs tend to be better optimized in SQL (particularly where, as in my example, you are using more than one on the same table), I just mention this alternative as it is a more direct translation of your pseudo-query.

新一帅帅 2024-12-28 19:27:47
  SELECT    * 
    FROM    Notification n
            LEFT OUTER JOIN Acknowledgment a ON a.parent_id = n.id
  WHERE     (a.parent_id IS NULL OR a.status = @somevalue)
  SELECT    * 
    FROM    Notification n
            LEFT OUTER JOIN Acknowledgment a ON a.parent_id = n.id
  WHERE     (a.parent_id IS NULL OR a.status = @somevalue)
柠北森屋 2024-12-28 19:27:47
SELECT n.* FROM Notification n
LEFT OUTER JOIN Acknowledgment a ON a.parent_id=n.id
WHERE a.status IS NULL OR a.status=@someValue
SELECT n.* FROM Notification n
LEFT OUTER JOIN Acknowledgment a ON a.parent_id=n.id
WHERE a.status IS NULL OR a.status=@someValue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文