如何使用select * with union? MySQL的

时间:2014-06-25 15:54:35

标签: mysql

我想运行此查询:

SELECT col1 , col2 , table1.*
    FROM
    (SELECT table2.col1 as col1 , table2.col2 as col2 , table1.* FROM table1 INNER JOIN table2 ...
    UNION
    SELECT table3.col1 as col1 , table2.col2 as col2 , table1.* FROM table1 INNER JOIN table2 ...
    )order by table1.col1

但是我无法运行它,因为我应该用table1列名替换table1。*(SELECT col1,col2,table1。*)。

2 个答案:

答案 0 :(得分:0)

您需要为联合查询指定别名。

类似的东西:

SELECT col1 , col2 , talias.*
FROM
(SELECT table2.col1 as col1 , table2.col2 as col2 , table1.* FROM table1 INNER JOIN table2 ...
UNION
SELECT table3.col1 as col1 , table2.col2 as col2 , table1.* FROM table1 INNER JOIN table2 ...
) as talias order by talias.col1

答案 1 :(得分:0)

我这里没有mysql,但是尝试在FROM部分添加table1并命名子查询

SELECT TableUnion.col1 , TableUnion.col2 , table1.*
    FROM
    (SELECT table2.col1 as col1 , table2.col2 as col2 , table1.* FROM table1 INNER JOIN table2 ...
    UNION
    SELECT table3.col1 as col1 , table2.col2 as col2 , table1.* FROM table1 INNER JOIN table2 ...
    ) as TableUnion, table1 order by table1.col1