user_unknown
user_unknown

Reputation: 195

How to create INNER JOIN multiple tables in sql

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.

Upvotes: 3

Views: 21502

Answers (4)

hugo hidalgo
hugo hidalgo

Reputation: 1

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.

Upvotes: 0

Marek Kwiendacz
Marek Kwiendacz

Reputation: 9824

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

Upvotes: 7

Ruben
Ruben

Reputation: 9186

or write 3 diffent select statements and join them with UNION

Upvotes: 0

Matt Asbury
Matt Asbury

Reputation: 5662

You can't use the same alias twice

Prices as pc

You can only use pc once.

Upvotes: 0

Related Questions