简体   繁体   中英

Multiple tables SQL with 1 same column

I have 2 tables:

acco_info
acco_revenue_2016

Both tables have different columns except for 1, acco_id. This column is what connects both tables.

I want to write a query that combines important data from both tables and links them to the acco_id .

So from the acco_info table I need the following columns:

acco_id, acco_name, region_name, country_name

From the acco_revenue_2016 table I need:

acco_id, sales, revenue_per_item, revenue

The output should look like this:

acco_id, acco_name, region_name, country_name, sales, revenue_per_item, revenue

What's the best way to write this query? I am stuck on

SELECT acco_id FROM acco_info UNION SELECT acco_id FROM acco_revenue_2016

This joins the ID's together but I can't find a way to also show the other data.

You'll be looking for something like this;

SELECT
     ai.acco_id
    ,ai.acco_name
    ,ai.region_name
    ,ai.country_name
    ,ar.sales
    ,ar.revenue_per_item
    ,ar.revenue
FROM acco_info ai
INNER JOIN acco_revenue_2016 ar
    ON ai.acco_id = ar.acco_id

This is assuming that both tables contain the same acco_id. Notice the table aliases to see which field is coming from each table

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