选择连接表中的字段在每行中相同的行

时间:2021-11-08 01:08:23

Suppose I have the following tables:

假设我有以下表格:

CREATE TABLE parents (
    id int primary key
);

CREATE TABLE children (
    parent_id int,  --id from parents
    day int,
    status bool,
}

INSERT INTO parents (id) VALUES (1);
INSERT INTO children (parent_id, day, status) VALUES (1, 1, TRUE);
INSERT INTO children (parent_id, day, status) VALUES (1, 2, TRUE);

INSERT INTO parents (id) VALUES (2);
INSERT INTO children (parent_id, day, status) VALUES (2, 1, TRUE);
INSERT INTO children (parent_id, day, status) VALUES (2, 2, FALSE);

INSERT INTO parents (id) VALUES (3);
INSERT INTO children (parent_id, day, status) VALUES (3, 1, TRUE); 

INSERT INTO parents (id) VALUES (4);
INSERT INTO children (parent_id, day, status) VALUES (4, 1, FALSE);

INSERT INTO parents (id) VALUES (5);

I need a query that will return:

我需要一个将返回的查询:

Parents
+------------+
|    id      |
+------------+
|     1      |
|     3      |
+------------+

where id is parents id. The resulting table only contains the parents that always(any day) true. Note that parents without children should be excluded.

其中id是父母id。结果表仅包含始终(任何一天)为真的父母。请注意,应排除没有孩子的父母。

My attempt:

我的尝试:

SELECT id
FROM parents p
INNER JOIN children c ON c.parent_id=p.id
WHERE c.status = TRUE
GROUP BY id

But it will also give parent with id=2.

但它也会给父母id = 2。

Another attempt:

另一种尝试:

SELECT id
FROM parents p
LEFT OUTER JOIN children c ON c.parent_id=p.id AND c.status=FALSE
WHERE c.status IS NULL
GROUP BY id

But this approach will also include parent with id=5, which must be excluded.

但是这种方法还包括id为5的父级,必须将其排除在外。

4 个解决方案

#1


8  

You don't need to join to parents.

您不需要加入父母。

SELECT parent_id
FROM children
GROUP BY parent_id
HAVING MIN(Status) = 'TRUE'
   AND MAX(Status) = 'TRUE'

No other Status besides TRUE.

除了TRUE之外没有其他状态。

#2


1  

SELECT id FROM parent P 
WHERE (P.id) IN
(SELECT c.parent_id FROM children c WHERE c.status = TRUE)

This will give you the desired result.

这将为您提供所需的结果。

#3


0  

This might also work

这也可能有用

SELECT DISTINCT p.id
  FROM parents p
 WHERE p.id IN ( 
                 SELECT c.parent_id
                   FROM children c
                  WHERE c.status = TRUE
                    AND c.parent_id = p.id 
               )

#4


0  

Use bit_add:

使用bit_add:

select a.id
from parents a
join children b on a.id = b.parent_id
group by a.id
having bit_and(b.status);

sqlfiddle

sqlfiddle

#1


8  

You don't need to join to parents.

您不需要加入父母。

SELECT parent_id
FROM children
GROUP BY parent_id
HAVING MIN(Status) = 'TRUE'
   AND MAX(Status) = 'TRUE'

No other Status besides TRUE.

除了TRUE之外没有其他状态。

#2


1  

SELECT id FROM parent P 
WHERE (P.id) IN
(SELECT c.parent_id FROM children c WHERE c.status = TRUE)

This will give you the desired result.

这将为您提供所需的结果。

#3


0  

This might also work

这也可能有用

SELECT DISTINCT p.id
  FROM parents p
 WHERE p.id IN ( 
                 SELECT c.parent_id
                   FROM children c
                  WHERE c.status = TRUE
                    AND c.parent_id = p.id 
               )

#4


0  

Use bit_add:

使用bit_add:

select a.id
from parents a
join children b on a.id = b.parent_id
group by a.id
having bit_and(b.status);

sqlfiddle

sqlfiddle