mySQL - 使用三个表中的数据和列创建新表 [英] mySQL - Create a New Table Using Data and Columns from Three Tables
本文介绍了mySQL - 使用三个表中的数据和列创建新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个新表,该表将包含来自三个现有表的所选数据。
现有的三个表设置如下,所需的结果位于底部:
人
id last_name first_name电子邮件
1史密斯弗雷德Fred @ ..
2琼斯汤姆汤姆@ ..
3 Doe简简@
taxonomy
id taxonomy
1 age
2 gender
3 height
详细信息
id person_id detail_id内容
1 1 1 36
2 1 2 M
3 1 3 5'10
4 2 1 29
5 2 2 M
6 2 3 6'3
7 3 1 27
8 3 2 F
9 3 3 5'8
新表
id last_name first_name email age
1 Smith Fred Fred @ .. 36
2 Jones Tom Tom @ .. 29
3 Doe Jane Jane @ .. 27
感谢您的帮助!
解决方案你需要做一个3-way JOIN:
CREATE TABLE new_table AS
SELECT p。*,d。内容AS age
FROM people AS p
JOIN详细信息AS d ON d.person_id = p.id
JOIN分类法AS t ON t.id = d.detail_id
WHERE t。 taxonomy ='age'
或者如果您已经创建了表格,可以执行:
INSERT INTO new_table(id,last_name,first_name,email,age)
SELECT p.id,p.last_name,p。 first_name,p.email,d.content AS age
FROM people AS p
JOIN详细信息AS d ON d.person_id = p.id
JOIN分类法AS t ON t.id = d。 detail_id
WHERE t.taxonomy ='age'
要获得多个属性,分别为每个属性分别加入细节和分类表:
CREATE TABLE new_table AS
SELECT p。*,d1 .content AS age,d2.content AS gender,d3.content AS height
FROM people AS p
JOIN详细信息AS d1 ON d1.person_id = p.id
JOIN分类AS t1 ON t1 .id = d1.detail_id
JOIN详细信息AS d2 ON d2.person_id = p.id
JOIN分类AS t2 ON t2.id = d2.detail_id
JOIN详细信息AS d3 ON d3.person_id = p.id
JOIN taxonomy AS t3 ON t3.id = d3.detail_id
WHERE t1.taxonomy ='age'AND t2.taxonomy ='gender'AND t3.taxonomy ='height'
I am trying to create a new table that will consist of selected data from three existing tables.
The three existing tables are set up as following, and my desired result is at the bottom:
people id last_name first_name email 1 Smith Fred Fred@.. 2 Jones Tom Tom@.. 3 Doe Jane Jane@.. taxonomy id taxonomy 1 age 2 gender 3 height details id person_id detail_id content 1 1 1 36 2 1 2 M 3 1 3 5'10" 4 2 1 29 5 2 2 M 6 2 3 6'3" 7 3 1 27 8 3 2 F 9 3 3 5'8" New Table id last_name first_name email age 1 Smith Fred Fred@.. 36 2 Jones Tom Tom@.. 29 3 Doe Jane Jane@.. 27
Thanks in advance for your help!
解决方案You need to do a 3-way JOIN:
CREATE TABLE new_table AS SELECT p.*, d.content AS age FROM people AS p JOIN details AS d ON d.person_id = p.id JOIN taxonomy AS t ON t.id = d.detail_id WHERE t.taxonomy = 'age'
Or if you've already created the table, you can do:
INSERT INTO new_table (id, last_name, first_name, email, age) SELECT p.id, p.last_name, p.first_name, p.email, d.content AS age FROM people AS p JOIN details AS d ON d.person_id = p.id JOIN taxonomy AS t ON t.id = d.detail_id WHERE t.taxonomy = 'age'
To get multiple attributes, you have to join with the details and taxonomy tables separately for each attribute:
CREATE TABLE new_table AS SELECT p.*, d1.content AS age, d2.content AS gender, d3.content AS height FROM people AS p JOIN details AS d1 ON d1.person_id = p.id JOIN taxonomy AS t1 ON t1.id = d1.detail_id JOIN details AS d2 ON d2.person_id = p.id JOIN taxonomy AS t2 ON t2.id = d2.detail_id JOIN details AS d3 ON d3.person_id = p.id JOIN taxonomy AS t3 ON t3.id = d3.detail_id WHERE t1.taxonomy = 'age' AND t2.taxonomy = 'gender' AND t3.taxonomy = 'height'
这篇关于mySQL - 使用三个表中的数据和列创建新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文