0

我有一张餐桌:

NAME     |   FOOD
-----------------
matthew  |   rice
matthew  |   beans
mark     |   rice
mark     |   beans
Luke     |   rice
john     |   beans

我需要提取只有大米的名称、只有豆类的名称以及两者兼有的名称。如果它们位于单独的表中,则 LEFT JOIN 将起作用。但我遇到了困难,因为他们在一张桌子上。

我已经尝试了这两个语句的变体,但没有运气:

SELECT name 
FROM diners 
WHERE NOT EXISTS 
    (SELECT name 
     FROM diners
     WHERE food = 'beans')

SELECT t1.name FROM diners AS t1
LEFT JOIN diners AS t2 ON t1.name = t2.name
WHERE t2.food = 'rice'
  AND t2.name IS NULL
4

5 回答 5

1

-- 两个都有的名字

SELECT NAME FROM diners
WHERE FOOD IN ('RICE','BEANS')
GROUP BY NAME
HAVING COUNT(*)>1

——只有大米的名字

SELECT NAME FROM diners
WHERE NAME NOT IN (
SELECT NAME FROM diners
WHERE FOOD NOT IN ('RICE'))

--只有豆子的名字

SELECT NAME FROM diners
WHERE NAME NOT IN (
SELECT NAME FROM diners
WHERE FOOD NOT IN ('BEANS'))
于 2012-06-21T09:21:14.083 回答
1

只吃米饭——

    select name from dinner where food='rice' and name in 
    (select name from diners group by name having count(name)=1);

只吃豆子——

   select name from dinner where food='beans' and name in 
   (select name from diners group by name having count(name)=1);

两者都有——

   select name from diners where food in ('rice','beans') group by name 
   having count(name)>1;
于 2012-06-21T09:28:35.987 回答
0
    create table #diners (name nvarchar(20), food nvarchar(20))
    insert into #diners values ('matthew','rice')
    insert into #diners values ('matthew','beans')
    insert into #diners values ('mark','rice')
    insert into #diners values ('mark','beans')
    insert into #diners values ('luke','rice')
    insert into #diners values ('john','beans')


    --  only rice
    select  d1.name
    from        #diners d1 
    where       d1.food = 'rice' and
            d1.name not in (select d2.name from #diners d2 where d2.food != 'rice')

    --  only beans
    select  d1.name
    from        #diners d1 
    where       d1.food = 'beans' and
            d1.name not in (select d2.name from #diners d2 where d2.food != 'beans')

    --  both
    select  d1.name
    from        #diners d1 
    where       d1.food = 'rice' and
            d1.name in (select d2.name from #diners d2 where d2.food = 'beans')
于 2012-06-21T09:10:18.063 回答
0

使用 GROUP_CONCAT 怎么样?

SELECT name GROUP_CONCAT(food) AS foodchain FROM diners GROUP BY name

然后,您可以检查食物链是否等于大米或豆类或您的应用程序中的任何内容。SQL 中可能有一个简单的解决方案,但这也应该有效。

于 2012-06-21T09:10:41.580 回答
0

要获得只吃过“豆子”的食客:

SELECT  distinct name 
FROM    DINERS D1
WHERE NOT EXISTS
(SELECT NULL FROM DINERS D2 WHERE FOOD = 'rice' AND D1.NAME = D2.NAME)
 AND EXISTS 
 (SELECT * FROM DINERS D2 WHERE FOOD = 'beans' AND D1.NAME = D2.NAME); 

为了让只吃“米饭”的食客:

SELECT  distinct name 
FROM    DINERS D1
WHERE NOT EXISTS
(SELECT NULL FROM DINERS D2 WHERE FOOD = 'beans' AND D1.NAME = D2.NAME)
 AND EXISTS 
 (SELECT * FROM DINERS D2 WHERE FOOD = 'rice' AND D1.NAME = D2.NAME);

只获得两者兼得的食客:

SELECT  distinct name 
FROM    DINERS D1
WHERE EXISTS
(SELECT * FROM DINERS D2 WHERE FOOD = 'rice' AND D1.NAME = D2.NAME)
AND EXISTS 
(SELECT * FROM DINERS D2 WHERE FOOD = 'beans' AND D1.NAME = D2.NAME);
于 2012-06-21T09:24:26.173 回答