如何使join语句的结果成为新表

时间:2017-07-05 09:09:28

标签: php mysql left-join

我有以下mysql查询。如何使查询结果成为新表;

SELECT t1.referree AS LEVEL1, t2.referree AS LEVEL2, t3.referree AS LEVEL3, t4.referree AS LEVEL4, t5.referree AS LEVEL5, t6.referree AS LEVEL6, t7.referree AS LEVEL7, t8.referree AS LEVEL8, t9.referree AS LEVEL9, t10.referree LEVEL10, t11.referree AS LEVEL11
                                        FROM referrals AS t1
                                        LEFT JOIN referrals AS t2 ON t2.referrer = t1.referree
                                        LEFT JOIN referrals AS t3 ON t3.referrer = t2.referree
                                        LEFT JOIN referrals AS t4 ON t4.referrer = t3.referree
                                        LEFT JOIN referrals AS t5 ON t5.referrer = t4.referree
                                        LEFT JOIN referrals AS t6 ON t6.referrer = t5.referree
                                        LEFT JOIN referrals AS t7 ON t7.referrer = t6.referree
                                        LEFT JOIN referrals AS t8 ON t8.referrer = t7.referree
                                        LEFT JOIN referrals AS t9 ON t9.referrer = t8.referree
                                        LEFT JOIN referrals AS t10 ON t10.referrer = t9.referree
                                        LEFT JOIN referrals AS t11 ON t11.referrer = t10.referree
                                        WHERE t1.referrer ='2'));

3 个答案:

答案 0 :(得分:1)

简单地创建view而不是table

视图只是一个视图,virtual table基于result-set的{​​{1}}。

SQL statement

Take look on documentation for more info about view

答案 1 :(得分:0)

您可以通过在CREATE TABLE语句末尾添加SELECT语句来创建另一个表:

CREATE TABLE new_tbl [AS] SELECT select_statement;

将select语句替换为上面的select语句。

参考:https://dev.mysql.com/doc/refman/5.7/en/create-table-select.html

编辑:正如jyothi在评论中建议的那样,在这种情况下,您可能想要使用View而不是新表。如果是这样的话,你可以使用这样的东西:

CREATE VIEW view_name AS SELECT select_statement;

参考:http://www.guru99.com/views.html

答案 2 :(得分:0)

假设所有列都是varchar(10)数据类型,请尝试以下语法:

CREATE TABLE new_table (Level1 varchar(10),Level2 varchar(10),Level3 varchar(10),Level4 varchar(10),Level5 varchar(10),Level6 varchar(10),Level7 varchar(10),Level8 varchar(10),Level9 varchar(10),Level10 varchar(10),Level11 varchar(10))
SELECT 
t1.referree AS LEVEL1, 
t2.referree AS LEVEL2,
 t3.referree AS LEVEL3, 
t4.referree AS LEVEL4, 
t5.referree AS LEVEL5, 
t6.referree AS LEVEL6, 
t7.referree AS LEVEL7, 
t8.referree AS LEVEL8, 
t9.referree AS LEVEL9, 
t10.referree LEVEL10, 
t11.referree AS LEVEL11 
FROM 
referrals AS t1 LEFT JOIN referrals AS t2 
ON t2.referrer = t1.referree 
LEFT JOIN referrals AS t3 
ON t3.referrer = t2.referree 
LEFT JOIN referrals AS t4 
ON t4.referrer = t3.referree 
LEFT JOIN referrals AS t5 
ON t5.referrer = t4.referree 
LEFT JOIN referrals AS t6 
ON t6.referrer = t5.referree 
LEFT JOIN referrals AS t7 
ON t7.referrer = t6.referree 
LEFT JOIN referrals AS t8 
ON t8.referrer = t7.referree 
LEFT JOIN referrals AS t9 
ON t9.referrer = t8.referree 
LEFT JOIN referrals AS t10 
ON t10.referrer = t9.referree 
LEFT JOIN referrals AS t11 
ON t11.referrer = t10.referree 
WHERE 
t1.referrer ='2'));