【问题标题】:Using Join operations in SQL在 SQL 中使用连接操作
【发布时间】:2021-11-09 08:12:25
【问题描述】:

我应该使用 SQL,但不了解联合运算符的用法。 我的目标是为每个冰淇淋检索 3 列:ice_cream_id、ice_cream_name、manufacturer_name。这些应该根据 ice_cream_id 升序排列。因此,例如,如果我有普通香草冰淇淋,我不想与所有 3 家制造商一起识别,而只想识别第一。

我了解 SELECT 和 FROM 命令,但不了解 WHERE: SELECT ice_cream_id, ice_cream_name,manufacturer_name FROM ice_cream,manufacturer 中的联合运算符。

此外,我应该检索制造成本大于 1 的所有冰淇淋的 id、名称、制造成本和制造商名称。我认为我也应该使用 WHERE 吗?

manufacturer
manufacturer_id  manufacturer_name  country
---------------  -----------------  ----------
1                Ben & Jerry's      Canada
2                4 Friends          Finland
3                Gelatron           Italy

ice_cream
ice_cream_id  ice_cream_name    manufacturer_id  manufacturing_cost
------------  ----------------  ---------------  ------------------
1             Plain Vanilla     1                1
2             Vegan Vanilla     2                0.89
3             Super Strawberry  2                1.44
4             Very plain        2                1.2



ingredient
ingredient_id  ingredient_name  kcal        protein     plant_based
-------------  ---------------  ----------  ----------  -----------
1              Cream            400         3           0
2              Coconut cream    230         2.3         1
3              Sugar            387         0           1
4              Vanilla extract  12          0           1
5              Strawberry       33          0.7         1
6              Dark chocolate   535         8           1


contains
ice_cream_id  ingredient_id  quantity
------------  -------------  ----------
1             1              70
1             3              27
1             4              3
2             2              74
2             3              21
2             4              5
3             1              60
3             3              10
3             5              30
4             2              95
4             4              5

【问题讨论】:

  • SELECT ... FROM t1 JOIN t2 ON t1.cx = t2.cy JOIN t3 ON t2.xv = t3.cw WHERE t1.ca = 123

标签: sql database join where-clause


【解决方案1】:
SELECT ice_cream_id, ice_cream_name, manufacturer_name from manufacturer JOIN 
contains ON manufacturer.ice_cream_id = contains.ice_cream_id LEFT JOIN ingredient 
ON contains.ingredient_id = ingredient.ingredient_id ORDER BY ice_cream_id ASC;

请试试这个,看看它是否有帮助。如果没有,请告诉我。我会帮你的。

【讨论】:

    【解决方案2】:

    通过连接,您可以合并两个或多个表中的行。

    在你的情况下:

    SELECT i.ice_cream_id, i.ice_cream_name, m.manufacturer_name
    FROM ice_cream i INNER JOIN manufacturer m ON i.manufacturer_id = m.manufacturer_id
    ORDER BY i.ice_cream_id;
    

    使用 INNER JOIN,您可以组合两个表中需要有值的行。所以NULL是无效的。

    【讨论】:

      【解决方案3】:

      您不需要在 where 子句中连接两个表。 这样做:

      SELECT ic.ice_cream_id, ic.ice_cream_name, mf.manufacturer_name 
      FROM ice_cream ic
      JOIN manufacturer mf on ic.manufacturer_id = mf.manufacturer_id
      

      在此示例中,ic 和 mf 是您使用的表的别名。 您可以使用不同的 JOIN 命令,具体取决于您想要执行的操作(例如 LEFT JOIN 或 RIGHT JOIN ...)。要连接两个表,请使用两个表中相同的列。有时您需要使用多个列来连接两个表...

      有了这些新信息,我相信您现在应该尝试自己做第二部分: “此外,我应该检索制造成本大于 1 的所有冰淇淋的 id、名称、制造成本和制造商名称。我想我也应该使用 WHERE 吗?”

      【讨论】:

      • 嗨@EljaSuhonen,我看到你到目前为止问的问题很少,但没有一个被标记为已回答,所以也许你可以检查一下:stackoverflow.com/help/someone-answersCheers!
      【解决方案4】:

      您可以使用这两个查询来获得您想要的结果集。

              --retrieve 3 columns for each ice cream
              SELECT ice.ice_cream_id,
                     ice.ice_cream_name,
                     man.manufacturer_name
              FROM manufacturer AS man
                  INNER JOIN ice_cream AS ice
                      ON ice.manufacturer_id = man.manufacturer_id
              ORDER BY ice.ice_cream_id ASC;
              ---Costs greater than 1
              SELECT *
              FROM
              (
                  SELECT man.manufacturer_id,
                         ice.ice_cream_name,
                         man.manufacturer_name,
                         SUM(ice.nufacturing_cost) AS manufacturing_cost
                  FROM manufacturer AS man
                      INNER JOIN ice_cream AS ice
                          ON ice.manufacturer_id = man.manufacturer_id
                  GROUP BY man.manufacturer_id,
                           man.manufacturer_name,
                           ice.ice_cream_name
              ) AS TotalCost
              WHERE TotalCost.manufacturing_cost > 1;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-30
        • 1970-01-01
        • 2021-08-02
        • 2014-07-22
        • 2015-12-12
        • 1970-01-01
        • 2011-01-01
        • 1970-01-01
        相关资源
        最近更新 更多