結合されたすべての値が別のテーブルにあるかどうかを知りたいです。
例:
id childId
1 2
1 3
1 4
2 6
2 7
2 8
2 9
childIds
2
3
4
6
7
**desired result:**
id allChildrenInChildIds
1 True
2 False
これを行うための最良の方法は何でしょうか?
このクエリを実行する場合
SELECT id, GROUP_CONCAT(childId)
FROM table
WHERE childId NOT IN (2,3,4,5,6,7,8)
GROUP BY id
結果のアイデアはすべて誤りになります。セットに含まれていないchildIdを判別できるように、GROUP_CONCATを追加しました。IDが結果に含まれていない場合、それはtrueです。
これを試して
SELECT
ID
,MIN(allChildrenInChildIds)
FROM
(
SELECT id,
CASE WHEN childId IN (SELECT childIds FROM Table2) THEN 'TRUE' ELSE 'FALSE'
END AS allChildrenInChildIds
FROM Table1
) result
GROUP BY ID
select id, case when sum(TrueorFalse) = count(1) then 'true'
else 'false' end
from (
select id, case when exists (select 1 from ChildIDs where id = childid) then 1
else 0
end as TrueOrFalse
from child ) A
group by A.id
存在する場合の使用(...から...を選択)
このようなもの?
select
id,
case when exists (
select c.childid
from Child as c
where c.id = ids.id
and not exists (
select *
from Childids
where c.childid = Childids.id
)) then 'No' else 'Yes' end as WereAllChildrenFound
from ids
これはどう ?
select id , case when count(c.childid) <> count(ci.childid) then 'false' else 'true' end as allChildrenInChildIds
from Child c
left join ChildIds ci
on c.childid = ci.childid
group by id