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

发布于 2024-10-07 08:20:36 字数 352 浏览 3 评论 0原文

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

price_id:product_name:vendor_name:price

类似:

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

但我没有成功。

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:

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.

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

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

发布评论

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

评论(4

软糯酥胸 2024-10-14 08:20:36

试试这个:

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

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
蛮可爱 2024-10-14 08:20:36

您不能使用同一别名两次

Prices as pc

。您只能使用 pc 一次。

You can't use the same alias twice

Prices as pc

You can only use pc once.

心清如水 2024-10-14 08:20:36

或者编写 3 个不同的 select 语句并用 UNION 将它们连接起来

or write 3 diffent select statements and join them with UNION

妄断弥空 2024-10-14 08:20:36

你好,我有 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

尝试根据自己的要求这样做。

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

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.

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