0

私はダイナーステーブルを持っています:

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

米のみを含む名前、豆のみを含む名前、およびその両方を抽出する必要があります。それらが別々のテーブルにある場合、LEFT JOINは機能します。しかし、それらが 1 つのテーブルにあるため、問題が発生しています。

これら2つのステートメントのバリエーションを試してみましたが、うまくいきませんでした:

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 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 に答える
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'))

--bean だけを持つ名前

SELECT NAME FROM diners
WHERE NAME NOT IN (
SELECT NAME FROM diners
WHERE FOOD NOT IN ('BEANS'))
于 2012-06-21T09:21:14.083 に答える
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 に答える