2

我想知道所有连接值是否都在另一个表中。

例子:

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

最好的方法是什么?

4

5 回答 5

1

尝试这个

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
于 2013-03-27T04:14:50.193 回答
1

SQL 小提琴演示


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

使用时存在(从...中选择...)

于 2013-03-27T04:57:41.453 回答
1

像这样的东西?

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
于 2013-03-27T05:29:10.390 回答
1

这个怎么样 ?


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 
于 2013-03-28T01:48:31.440 回答
1

如果您运行此查询

SELECT id, GROUP_CONCAT(childId) 
FROM  table 
WHERE childId NOT IN (2,3,4,5,6,7,8) 
GROUP BY id

结果中的任何想法都是错误的。我添加了 GROUP_CONCAT,以便您可以确定哪些 childId 不在集合中。如果 id 不在结果中,则为 true。

于 2013-03-27T03:54:30.553 回答