开发者

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.


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


You can't use the same alias twice

Prices as pc

You can only use pc once.


or write 3 diffent select statements and join them with UNION


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜