从一个表中选择唯一数据

时间:2012-06-21 09:00:53

标签: sql database select left-join

我有一个食客桌:

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

5 个答案:

答案 0 :(得分: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'))

答案 1 :(得分: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;

答案 2 :(得分: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')

答案 3 :(得分:0)

如何使用GROUP_CONCAT?

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

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

答案 4 :(得分: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);