简体   繁体   中英

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.

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM