为以下表创建SQL查询

时间:2015-02-19 10:56:02

标签: python sql

数据库由4个表组成:Table1,table2,table3和table4。 enter image description here


对于给定的查询 Q If Q = Name_1。我想选择表 1 2 3 4 中的所有字段并将它们保存在一个数组中使用python。


创建表格:

Create Table table1 (ID1 INT PRIMARY KEY NOT NULL, NAME VARCHAR(100));
Create table table2 (ID2 INT PRIMARY KEY NOT NULL, NAME VARCHAR(100),ID_T1 INT, Foreign Key(ID_T1) references table1(ID1));
Create table table3 (ID3 INT PRIMARY KEY NOT NULL, NAME VARCHAR(100),ID_T1 INT, Foreign Key(ID_T1) references table1(ID1));
Create table table4 (ID4 INT PRIMARY KEY NOT NULL, NAME VARCHAR(100),ID_T2 INT, Foreign Key(ID_T2) references table2(ID2));

在表格中插入数据:

insert into table1 (ID1,NAME) values ("1", "john");
insert into table2 (ID2,NAME,ID_T1) values ("1", "math","1");
insert into table3 (ID3,NAME,ID_T1) values ("1", "physics","1");
insert into table4 (ID4,NAME,ID_T2) values ("1", "friend of","1");

1 个答案:

答案 0 :(得分:1)

以下是查询:

 SELECT * FROM Table1 AS t1 
    JOIN table3 as t3 ON t1.ID_table1 = t3.ID_table1
    JOIN table2 as t2 ON t1.ID_table1 = t2.ID_table1
    JOIN table4 as t4 ON t2.ID_table2 = t4.ID_table2