gpt4 book ai didi

mysql - 连接两个表并从结果中进行选择

转载 作者:行者123 更新时间:2023-11-30 00:47:37 25 4
gpt4 key购买 nike

我正在尝试 SQL,但在组合多个表中的信息时无法确定查询。一直在使用w3school但他们似乎没有对下面的问题有类似的引用。正在考虑将它们作为我下面的代码加入,但这仍然不能回答问题。感谢任何建议。谢谢。

Question:

Products(itemID, description, quantity, supplierID)

Supplier(supplierID, name, address)

A product can be supplied by more than one supplier. Write the SQL to list the quantity of each product by each supplier.

SELECT Products.quanity, Supplier.name
FROM Products INNER JOIN Supplier
ON Products.supplierID = Supplier.supplierID;

最佳答案

假设 product 表没有同一供应商和产品的两行,那么您的查询非常接近。我会把它写成:

SELECT p.description as ProductDescription, s.name as SupllierName, p.Quantity
FROM Products p INNER JOIN
Supplier s
ON p.supplierID = s.supplierID
ORDER BY p.description, p.Quantity desc;

注意以下几点:

  • 使用表别名(ps)使查询更具可读性,并且可用于每个列引用。
  • 最终结果明确按产品排序,数量最大的在前。
  • 如果给定产品和供应商有多行,那么您将需要聚合。

关于mysql - 连接两个表并从结果中进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21224246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com