SQL查询存在问题

发布于 2024-10-04 15:55:23 字数 2502 浏览 5 评论 0原文

我在我需要的最后一个查询上遇到了很多麻烦,我认为这是我无法达到的水平,所以任何帮助都是值得赞赏的。

Food

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是仓库存储的数量。

Contains 中的Quantity 是配方中使用的食物元素所需的量。

Food 表中的 QuantityContains 彼此不同。

查询:

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

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

编辑:另外,如果 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

因为它是仓库中仅有的两个有足够数量的菜谱。

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

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

I'm having a lot of troubles with the last query I need and I think it's a level out of my league so any help is appreciated.

The tables:

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 in Food table is the quantity stored in warehouse.

Quantity in Contains is the amount needed of a food element to use in recipe.

Quantity in Food table and Contains differ from each other.

The query:

I want to know the names of ALL recipes that are possible to be done with the food stored in warehouse.

It requires that the quantity of every element of food in warehouse is bigger than the quantity needed for the recipe.

EDIT: also, it shouldn't show a recipe's name if there is nothing referring to it on Contains table.

To make it easier to understand, I'll give some data:

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);

The outcome expected from the query is:

Rice and ice tea
Steak with rice

Since it's the only two recipes with enough quantity in warehouse.

EDIT: potatoes with rice shouldn't appear as it is a recipe but isn't in contains list

Thanks for input and time. Any help is welcome :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦魇绽荼蘼 2024-10-11 15:55:23

我会使用 >= 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);

正确的拼写是recipe,并且您对某些列使用了不同的名称(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);

I'd use >= ALL operator :

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);

The correct spelling is recipe, and you used different names for some columns (recepie, nrecipe, nrecepie) so I changed it. Note that instead of using a varchar primary key, you should use a numeric one.

Edit:

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);
伴我老 2024-10-11 15:55:23

这是在 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
            )
    )

用简单的语言来说就是“给我所有没有不足数量成分的食谱”

This is in SQL Server because that is what I have:

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
            )
    )

Which in plain language is "Get me all recipes where there are no ingredients of insufficient quantity"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文