JayKandari
JayKandari

Reputation: 1248

Get all Columns of One table having one column which has to derive data from another table corresponding to its data

Consider a table

PRODUCTSTABLE

product_id | product_name | product_country
1          | ABC          | 1    
2          | DEF          | 3    
3          | ASD          | 2    
4          | JGH          | 3    
5          | WER          | 2

COUNTRY TABLE

country_id | country_name
1          | Japan
2          | China
3          | Uganda
4          | France
5          | United States

I want to get results like as this query would produce

SELECT * FROM PRODUCTSTABLE;

The only difference would be in the third column 'product_country', instead of number respective Country name referenced from the second table must come.

Thank you.

Upvotes: 0

Views: 78

Answers (2)

MartinB
MartinB

Reputation: 496

What about:

SELECT 
  A.Product_ID, A.Product_Name, B.Country_Name 
FROM PRODUCTSTABLE A 
LEFT JOIN Country_Table B on A.Product_Country = B.Country_ID

try this article: http://en.wikipedia.org/wiki/Join_(SQL)

Upvotes: 1

John Woo
John Woo

Reputation: 263803

You need to join both tables using INNER JOIN.

SELECT  a.product_id,
        a.product_name,
        b.country_name
FROM    products a
        INNER JOIN country b
            ON a.product_country = b.country_ID

To further gain more knowledge about joins, visit the link below:

Upvotes: 1

Related Questions