MySQL 如何在sql中创建INNER JOIN多个表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4409919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:57:20  来源:igfitidea点击:

How to create INNER JOIN multiple tables in sql

sqlmysqlselectinner-join

提问by user_unknown

I have 3 tables: Products, Vendors and Prices. Prices has product_id and vendor_id as foreign keys. Now i want to show Prices as:

我有 3 个表:产品、供应商和价格。Prices 有 product_id 和 vendor_id 作为外键。现在我想将价格显示为:

price_id:product_name:vendor_name:price

price_id:product_name:vendor_name:price

Something like:

就像是:

SELECT p.product, v.vendor, pc.price
FROM Products AS p,
Vendors AS v
INNER JOIN Prices AS pc
ON p.product_id = pc.product_id
INNER JOIN Prices AS pc
ON v.vendor = pc.vendor_id

but I didnt get it work.

但我没有得到它的工作。

回答by Marek Kwiendacz

Try this:

尝试这个:

SELECT pr.price_id, p.product_name v.vendor_name, pr.price
FROM Prices AS pr
LEFT JOIN Products AS p ON p.product_id = pr.product_id
LEFT JOIN Vendors AS v ON v.vendor = pr.vendor_id

回答by Matt Asbury

You can't use the same alias twice

您不能两次使用相同的别名

Prices as pc

You can only use pc once.

您只能使用一次电脑。

回答by Ruben

or write 3 diffent select statements and join them with UNION

或编写 3 条不同的 select 语句并使用 UNION 加入它们

回答by hugo hidalgo

Hello I have 3 different tables, and this query works well.

您好,我有 3 个不同的表,此查询运行良好。

SELECT A.ID_USER,A.NAME,D.ADDRESS,B.ID_STATE,B.STATE,A.ID_COUNTRY,C.COUNTRY 
FROM market.USER A 
    INNER JOIN market.state B
ON   A.ID_STATE=B.ID_STATE 
    INNER JOIN market.country C
ON A.ID_COUNTRY=C.ID_COUNTRY
    INNER JOIN market.contact D
ON A.ID_CONTACT=D.ID_CONTACT

try to do that for your own requirement.

尝试根据您自己的要求执行此操作。