如果UNION和CREATE TABLE共存

时间:2016-09-29 01:02:55

标签: mysql union

在mysql Distrib 5.5.49中,有2个表,当具有不同ID的数据内容相同时,我们正在尝试回收数据(复制除ID列之外的所有列)。

请参阅小提琴,例如:

http://sqlfiddle.com/#!9/91c1cf/5

问题是以下查询在单独运行时运行完全正常:

SELECT Id, Name, Score FROM MAIN

UNION

SELECT d.New_Id AS Id, c.Name, c.Score FROM MAIN c
RIGHT JOIN SUB d

ON c.Id = d.Id 

但是,当它与CREATE TABLE命令结合使用时,会弹出一条错误消息:

CREATE TABLE IF NOT EXISTS TEST (

SELECT Id, Name, Score FROM MAIN

UNION

SELECT d.New_Id AS Id, c.Name, c.Score FROM MAIN c
RIGHT JOIN SUB d

ON c.Id = d.Id );

错误:

ERROR 1064 (42000) at line 5: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION

事实证明,MySQL可能不会与CREATE TABLE一起支持UNION?这里的用户手册不是很清楚。

https://dev.mysql.com/doc/refman/5.6/en/create-table.html

任何一位大师能开导吗?

0 个答案:

没有答案