简体   繁体   English

SQL:选择所有连接记录满足某些条件的记录

[英]SQL: Select records where ALL joined records satisfy some condition

How can I write an SQL query that returns a record only if ALL of the associated records in a joined table satisfy some condition. 只有当连接表中的所有关联记录满足某些条件时,如何编写仅返回记录的SQL查询。

For example, if A has many B, I want to SELECT * FROM A WHERE all related B's for a given A have B.some_val > value 例如,如果A有很多B,我想SELECT * FROM A WHERE给定A的所有相关B都有B.some_val>值

I know this is probably a pretty basic question, so thanks for any help. 我知道这可能是一个非常基本的问题,所以感谢您的帮助。 Also, if it makes a difference, I'm using postgres. 此外,如果它有所作为,我正在使用postgres。

Sam 山姆

Assuming no need for correlation, use: 假设不需要关联,请使用:

SELECT a.*
  FROM A a
 WHERE EXISTS(SELECT NULL
                FROM B b
              HAVING MIN(b.some_val) > a.val)

If you do need correlation: 如果你确实需要相关性:

SELECT a.*
  FROM A a
 WHERE EXISTS(SELECT NULL
                FROM B b
               WHERE b.id = a.id
              HAVING MIN(b.some_val) > a.val)

Explanation 说明

The EXISTS evaluates on a boolean, based on the first match - this makes it faster than say using IN, and -- unlike using a JOIN -- will not duplicate rows. EXISTS根据第一个匹配对布尔值进行求值 - 这比使用IN更快,而且 - 与使用JOIN不同 - 不会重复行。 The SELECT portion doesn't matter - you can change it to EXISTS SELECT 1/0 ... and the query will still work though there's an obvious division by zero error. SELECT部分​​无关紧要 - 您可以将其更改为EXISTS SELECT 1/0 ...并且查询仍然有效,尽管存在明显的零错误除法。

The subquery within the EXISTS uses the aggregate function MIN to get the smallest B.some_val - if that value is larger than the a.val value, the a.val is smaller than all of the b values. EXISTS的子查询使用聚合函数MIN来获得最小的B.some_val - 如果该值大于a.val值,则a.val小于所有b值。 The only need for a WHERE clause is for correlation - aggregate functions can only be used in the HAVING clause. WHERE子句的唯一需要是用于关联 - 聚合函数只能在HAVING子句中使用。

select * from A
 where -- at least one record in B is greater than some_val
       exists (select null from B
                where B.some_val > :value
                  and A.join_column = B.join_column)
   and -- no records in B are not greater than some_val
       not exists (select null from B
                    where B.some_val <= :value
                      and A.join_column = B.join_column)

The following should work: 以下应该有效:

SELECT *
FROM a
JOIN b ON a.key = b.key AND a.value > b.value

Because this does an inner join and not a outer join , records from A will only be included if they have records in B that satisfy the condition. 因为它执行内连接而不是外连接 ,所以只有在B中满足条件的记录时,才会包含A中的记录。

I don't use PostGRE, so I can't guarantee that the syntax is exactly correct. 我不使用PostGRE,所以我无法保证语法完全正确。

You are wanting an INNER JOIN: 你想要一个INNER JOIN:

SELECT
    A.*
FROM
    A
INNER JOIN B
    ON A.identifier = B.identifier
WHERE
    B.some_val > value

You will want to ensure that there is a foreign key from A to B, or some other common identifier. 您需要确保存在从A到B的外键或其他常用标识符。

select * from a where a.key = b.a_key where b.value > condition

Use bookstores and books as an example. 以书店和书籍为例。

Bookstore 书店

bookstoreID, bookID bookstoreID,bookID

Book

bookID, price bookID,价格

I suppose you want to return all the bookstores in which all the books have a price greater than X. 我想你想要归还所有书籍价格都高于X的书店。

select *
from Bookstore bs1
where bs1.bookstoreID not exist
 (
  select bs.bookstoreID
  from Bookstore bs, Book b
  where bs.bookID= b.bookID
  b.price < x;   -- your value                                                     
 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM