存在SQL查询问题

时间:2010-11-26 01:18:11

标签: sql exists

我在上次查询时遇到了很多麻烦,我认为这是我联盟中的一个级别,所以任何帮助都会受到赞赏。

表格:

CREATE TABLE Recipe
(
     nrecipe     integer, 
     name         varchar(255),
     primary key (nrecipe)
);

CREATE TABLE Food
(
    designation  varchar(255)   unique,
    quantity     integer,
    primary key (designation)
);

CREATE TABLE Contains
(
     nrecipe      integer,
     designation  varchar(255),
     quantity     integer,
     primary key (nrecipe, designation),
     foreign key (nrecipe) references Recepie (nrecipe),
     foreign key (designation) references Food (designation)
);
Quantity表中的

Food是存储在仓库中的数量。

Quantity中的{p> Contains是食谱中食物元素所需的量。

Quantity表格中的

FoodContains彼此不同。

查询:

我想知道可以用仓库中存储的食物完成的所有配方的名称。

它要求仓库中每种食物元素的数量都大于食谱所需的数量。

编辑:另外,如果在Contains表上没有任何内容引用,则不应显示食谱的名称。

为了便于理解,我将提供一些数据:

INSERT INTO Recipe  VALUES ('01', 'Steak with potatos and water');
INSERT INTO Recipe  VALUES ('02', 'Rice and ice tea');
INSERT INTO Recipe  VALUES ('03', 'Potatos and shrimp');
INSERT INTO Recipe  VALUES ('04', 'Water');
INSERT INTO Recipe  VALUES ('05', 'Steak with rice');
INSERT INTO Recipe  VALUES ('06', 'Steak with spaguetti');
INSERT INTO Recipe  VALUES ('07', 'Potatos with rice');

INSERT INTO Food    VALUES ('Water', 5);
INSERT INTO Food    VALUES ('Ice tea', 10);
INSERT INTO Food    VALUES ('Steak', 30);
INSERT INTO Food    VALUES ('Potatos', 20);
INSERT INTO Food    VALUES ('Rice', 50);
INSERT INTO Food    VALUES ('Shrimp', 5);
INSERT INTO Food    VALUES ('Spaguetti', 5);

INSERT INTO Contains    VALUES ('01', 'Steak', 1);
INSERT INTO Contains    VALUES ('01', 'Potatos', 15);
INSERT INTO Contains    VALUES ('01', 'Water', 10);
INSERT INTO Contains    VALUES ('02', 'Rice', 5);
INSERT INTO Contains    VALUES ('02', 'Ice tea', 8);
INSERT INTO Contains    VALUES ('03', 'Potatos', 1);
INSERT INTO Contains    VALUES ('03', 'Shrimp', 10);
INSERT INTO Contains    VALUES ('04', 'Water', 20);
INSERT INTO Contains    VALUES ('05', 'Steak', 1);
INSERT INTO Contains    VALUES ('05', 'Rice', 20);
INSERT INTO Contains    VALUES ('06', 'Steak', 1);
INSERT INTO Contains    VALUES ('06', 'Spaguetti', 10);

查询预期的结果是:

Rice and ice tea
Steak with rice

因为它是仓库中数量足够的两个配方。

编辑:土豆和米饭不应该出现,因为它是一个食谱,但不在包含列表中

感谢您的投入和时间。欢迎任何帮助:)

2 个答案:

答案 0 :(得分:2)

我使用>= ALL运算符:

SELECT name
  FROM Recipe R
 WHERE 0 >= ALL (SELECT C.quantity - F.quantity
                   FROM Food F
             INNER JOIN Contains C
                  USING (designation)
                  WHERE C.nrecipe = R.nrecipe);

正确的拼写是配方,你为某些列(recepie,nrecipe,nrecepie)使用了不同的名称,所以我改变了它。请注意,不应使用varchar主键,而应使用数字键。

修改

SELECT name
  FROM Recipe R
 WHERE 0 >= ALL (SELECT C.quantity - F.quantity
                   FROM Food F
             INNER JOIN Contains C
                  USING (designation)
                  WHERE C.nrecipe = R.nrecipe)
   AND EXISTS(SELECT NULL
                FROM Contains C
               WHERE C.nrecipe = R.nrecipe);

答案 1 :(得分:0)

这是在SQL Server中,因为这就是我所拥有的:

select
    r.name
from
    Recepie r
where
    not exists 
    (
        select 1
        from
            [Contains] c
        where
            c.nrecipe = r.nrecepie and
            not exists 
            (
                select 1
                from
                    Food f
                where
                    f.designation = c.designation and
                    f.quantity >= c.quantity
            )
    )

用简单的语言是“在没有数量不足的成分的情况下获取所有食谱”