Chandrasekar
Chandrasekar

Reputation: 35

How to get names of relevant ID's from 2 different tables and display them

I have 3 tables, First one has the product ID and Name, Second one has Supplier ID and name, In the 3rd one i have product ID and Supplier ID. While displaying, i want to replace the product ID and supplier ID in the 3rd table with product name and supplier name from the 1st and 2nd table respectively. Please let me know the query for executing it.

Upvotes: 1

Views: 1145

Answers (2)

sealocal
sealocal

Reputation: 12407

Reference: http://dev.mysql.com/doc/refman/5.7/en/join.html

SELECT * FROM table1
INNER JOIN table2
ON table1.id=table2.id
INNER JOIN table3
ON table2.id=table3.id;

If your tables are named products_master, stockists_master, and stockist_product_offer, then you can join the tables and select any of the six columns that you want.

SELECT product_master.name, stockists_master.name
FROM products_master
INNER JOIN stockist_product_offer
ON product_master.id = stockist_product_offer.product_id
INNER JOIN stockists_master
ON stockist_product_offer.stockist_id = stockist.id;

Upvotes: 1

indian
indian

Reputation: 130

you have to join the tables on IDs so the query is : let s say that : * first table : product * second table : supplier * third table : match

SELECT P.PRODUCTNAME
       S.SUPPLIERNAME
FROM
       PRODUCT P
INNER JOIN
       MATCH M
ON     P.PRODUCTID = M.PRODUCTID  
INNER JOIN 
       SUPPLIER S
ON     S.SUPPLIERID = M.SUPPLIERID
ORDER BY  1
;

Upvotes: 0

Related Questions