选择所有关系数据

时间:2018-04-16 05:58:53

标签: sql postgresql

如果我有一个 1到N 的关系,那么products表是这样的:

  products
+----+------+
| id | name |
+----+------+
| 1  | foo  |
| 2  | bar  |
+----+------+

options这样的表:

  options
+----+------------+--------+--------+
| id | product_id | option | active |
+----+------------+--------+--------+
| 10 | 1          | size   | true   |
| 11 | 1          | color  | false  |
| 12 | 2          | size   | true   |
| 13 | 2          | color  | true   |
+----+------------+--------+--------+

如何通过products获取具有所有选项的active = true?请注意,如果某个产品至少有一个active = false选项,那么我不希望该查询检索该产品。

在这种情况下,想要的结果将是产品2。

7 个答案:

答案 0 :(得分:2)

您可以使用子查询从选项表中获取所有活动的product_ids,使用Postgres的布尔聚合bool_and,如果所有值均为true

,则仅返回true
select *
from products
where id in (select product_id
             from options
             group by product_id
             having bool_and(active));

在线示例:http://rextester.com/ILPX66228

答案 1 :(得分:0)

您只需使用not exists运算符

即可
select p.* from  products p
where not exists (select 1 from options where product_id = p.id and active = false)

这假定active具有boolean类型,否则您需要使用单引号(即'string')进行比较

答案 2 :(得分:0)

我猜你正在寻找这个

SELECT  P.id, P.name, O.option, O.active from products P
INNER JOIN options O
ON P.id = O.product_id
WHERE active <> 'false'

答案 3 :(得分:0)

这对你有用......

Select distinct P.name from products P 
join options O on O.id=P.id
where O.active=true

答案 4 :(得分:0)

您可以将GROUP BYHAVING子句一起使用条件聚合:

SELECT p.id, p.name
FROM products AS p
LEFT JOIN options AS o ON ON p.id = o.product_id
GROUP BY p.id, p.name
HAVING COUNT(CASE WHEN active = true THEN 1 END) = COUNT(*)

答案 5 :(得分:0)

类似这样的事情

 select * from products 
 where id not in (select product_id from options where active = false)

答案 6 :(得分:0)

将GROUP BY与HAVING子句一起使用:

SELECT pr.id, pr.name
FROM products AS pr
LEFT JOIN options AS op ON ON pr.id = op.product_id
GROUP BY pr.id
HAVING COUNT(CASE WHEN active = false THEN 1 else 0 END) <= 0