SQL 通过使用第三个连接两个表来创建表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7125495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create tables by JOINing two tables using a third
提问by joiner
I have three tables:
我有三个表:
TABLE 1 (7.7million records)
表 1(770 万条记录)
ID_1|..|..| OTHER FIELDS|
ID_1|..|..| OTHER FIELDS|
TABLE 2 (8.2 million records)
表 2(820 万条记录)
ID_2 |..|..|.....
ID_2 |..|..|.....
TABLE 12 (7.5 million records)
表 12(750 万条记录)
ID_1| ID_2 | SOMEFIELDS|
AND ID_1== ID_2.i.e.
contains all common ids
ID_1| ID_2 | SOMEFIELDS|
ANDID_1== ID_2.i.e.
包含所有常见的ids
The table 12
contains unique ids
which are common to table 1
and 2
.
I am trying create a new table to get all data from t1
and t2
by matching which records in t12
with id_1,id_2
).
该table 12
含有独特的ids
这是共同的table 1
和2
。我试图创建一个新表,从获取的所有数据t1
,并t2
通过在记录匹配t12
带id_1,id_2
)。
Following is the sql Iam using:
以下是我使用的 sql:
CREATE TABLE ARROW_all_common12 AS
SELECT T1.*, T2.* FROM T1, T2
LEFT JOIN T12
ON T12.ID_1=T1.ID_1
LEFT JOIN T12
ON T12.ID_2 = T2.ID_2
WHERE T12.ID2 = T2.ID_2
回答by bhamby
I'm not entirely sure what you're asking here, but maybe a view could be what you're looking for?
我不完全确定你在这里问的是什么,但也许一个视图可能是你正在寻找的?
CREATE VIEW someview AS (
SELECT t1.*, t2.*
FROM table12 AS t12
INNER JOIN table1 AS t1
ON t1.id_1 = t12.id1
INNER JOIN table2 AS t2
ON t12.id_2 = t2.id_2
)
回答by roselan
same, it's not quite clear. maybe that?
一样的,不是很清楚。也许那个?
create table t_all_12 as (
select t1.*, t2.*
from t1, t2, t12
where t12.id_1 = t1.id_1
and t12.id_2 = t1.id_2
)