从3个或更多不同的表

时间:2015-06-30 14:29:44

标签: mysql database

我有2个主要表,Table1和Table2:

表1

  • Attrib_Alpha
  • Attrib_1
  • Attrib_2
  • Attrib_3
  • Attrib_4
  • Attrib_5

表2

  • Attrib_Bravo
  • Attrib_6
  • Attrib_7
  • Attrib_8
  • Attrib_9

我将使用这2个查询仅选择2个表的一些记录($ Attrib_变量包含要搜索的记录):

SELECT Attrib_Alpha FROM Table1 WHERE Attrib_1 IN (".$Attrib_1.") AND Attrib_2 IN (".$Attrib_2.") AND Attrib_3 IN (".$Attrib_3.") AND Attrib_4 IN (".$Attrib_4.") AND Attrib_5 IN (".$Attrib_5.")

SELECT Attrib_Bravo FROM Table2 WHERE Attrib_6 IN (".$Attrib_6.") AND Attrib_7 IN (".$Attrib_7.") AND Attrib_8 IN (".$Attrib_8.") AND Attrib_9 IN (".$Attrib_9.")

这两个查询输出将填入2个变量($ List_Alpha和$ List_Bravo)。这两个数组用于查询其他表tbl1,tbl2,tbl3,tbl4,tbl5,tbl6(这些表具有相同的结构)。

tbl1,tbl2,tbl3,tbl4,tbl5,tbl6

  • Attrib_Alpha
  • Attrib_Bravo
  • Elem_1
  • Elem_2
  • Elem_3
  • Elem_4
  • Elem_5

以下代码将直接创建Final_Table:

CREATE TABLE Final_Table SELECT * FROM tbl1,tbl2,tbl3,tbl4,tbl5,tbl6 WHERE Attrib_Alpha IN (".$List_Alpha.") AND Attrib_Bravo IN (".$List_Bravo.") AND Elem_1 IN (".$Elem_1.") AND Elem_2 IN (".$Elem_2.") AND Elem_3 IN (".$Elem_3.") AND Elem_4 IN (".$Elem_4.") AND Elem_5 IN (".$Elem_5.")

Final_Table的结构如下:

Final_Table

  • Attrib_Alpha
  • Attrib_Bravo
  • Elem_1
  • Elem_2
  • Elem_3
  • Elem_4
  • Elem_5

我正在尝试将Table1和Table2中的相应属性添加到Final_Table中,以便它看起来像这样:

  • Attrib_Alpha
  • Attrib_Bravo
  • Elem_1
  • Elem_2
  • Elem_3
  • Elem_4
  • Elem_5
  • Attrib_1
  • Attrib_2
  • Attrib_3
  • Attrib_4
  • Attrib_5
  • Attrib_6
  • Attrib_7
  • Attrib_8
  • Attrib_9

我想添加另一个查询来执行此操作:

UPDATE TABLE Final_Table SELECT * FROM Table1 WHERE Attrib_Alpha IN (".$List_Alpha.")
UPDATE TABLE Final_Table SELECT * FROM Table2 WHERE Attrib_Bravo IN (".$List_Bravo.")

但它没有用。我确信有更快的方法来完成所有这些,可能使用更复杂的查询,但请记住tbl1,tbl2,tbl3,tbl4,tbl5,tbl6是大表(每个> 800Mb)

0 个答案:

没有答案