简体   繁体   English

具有1个相同列的多个表SQL

[英]Multiple tables SQL with 1 same column

I have 2 tables: 我有2张桌子:

acco_info
acco_revenue_2016

Both tables have different columns except for 1, acco_id. 两个表都有不同的列,除了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 . 我想编写一个查询,它结合了两个表中的重要数据,并将它们链接到acco_id

So from the acco_info table I need the following columns: 所以从acco_info表我需要以下列:

acco_id, acco_name, region_name, country_name

From the acco_revenue_2016 table I need: 从acco_revenue_2016表我需要:

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. 这将ID加在一起,但我找不到同时显示其他数据的方法。

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. 这假设两个表都包含相同的acco_id。 Notice the table aliases to see which field is coming from each table 请注意表别名以查看来自每个表的字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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