多个UNION查询:
SELECT [Ordine numero] AS ordine, [data ordine] AS data, comm AS commessa
FROM [archivio globale]
WHERE [ordine numero] IS NOT NULL
UNION
SELECT [numero ordine cliente] AS ordine, [data ordine cliente] AS data, numero AS commessa
FROM [ricambi]
WHERE [numero ordine cliente] IS NOT NULL
UNION
SELECT [numero ordine cliente] AS ordine, [data ordine cliente] AS data, numero AS commessa
FROM [trasferte]
WHERE [numero ordine cliente] IS NOT NULL
ORDER BY [ordine];
它不起作用:我收到错误消息:
[mysql][odbc 5.1 Driver][mysqld-5.5.14]You have an error in your SQL syntax ...
虽然单个UNION查询正常工作(没有第二个UNION语句)?怎么办?
答案 0 :(得分:1)
您已将问题标记为MySQL,并使用方括号[]
。据我所知,方括号不对MySQL有效,仅对Microsoft产品有效(SQL Server / MS Access)。因此,如果您需要附加表/列名称,请使用反引号`
。
标识符引号字符是反引号(“`”):
所以我认为您的查询应该是:
SELECT `Ordine numero` AS ordine, `data ordine` AS data, comm AS commessa
FROM `archivio globale`
WHERE `ordine numero` IS NOT NULL
UNION ALL
SELECT `numero ordine cliente` AS ordine, `data ordine cliente` AS data, numero AS commessa
FROM `ricambi`
WHERE `numero ordine cliente` IS NOT NULL
UNION ALL
SELECT `numero ordine cliente` AS ordine, `data ordine cliente` AS data, numero AS commessa
FROM `trasferte`
WHERE `numero ordine cliente` IS NOT NULL
ORDER BY `ordine`;
编辑,如果您使用的是MS Access,则需要使用方括号:
SELECT *
FROM
(
SELECT [Ordine numero] AS ordine, [data ordine] AS data, comm AS commessa
FROM [archivio globale]
WHERE [ordine numero] IS NOT NULL
UNION ALL
SELECT [numero ordine cliente] AS ordine, [data ordine cliente] AS data, numero AS commessa
FROM [ricambi]
WHERE [numero ordine cliente] IS NOT NULL
UNION ALL
SELECT [numero ordine cliente] AS ordine, [data ordine cliente] AS data, numero AS commessa
FROM [trasferte]
WHERE [numero ordine cliente] IS NOT NULL
) x
ORDER BY [ordine];
答案 1 :(得分:1)
这是MyODBC的错误,请参阅offical entry here。截至今天,它仍然开放......
[编辑]我找到了一个简单的解决方案:myODBC只能理解一个UNION
(或UNION ALL
)的查询。所以你必须用UNION
包裹()
的右边部分。另外,您需要使用;
结束每个查询,如下所示:
SELECT [Ordine numero] AS ordine, [data ordine] AS data, comm AS commessa
FROM [archivio globale]
WHERE [ordine numero] IS NOT NULL;
UNION ALL (
SELECT [numero ordine cliente] AS ordine, [data ordine cliente] AS data, numero AS commessa
FROM [ricambi]
WHERE [numero ordine cliente] IS NOT NULL;
UNION ALL
SELECT [numero ordine cliente] AS ordine, [data ordine cliente] AS data, numero AS commessa
FROM [trasferte]
WHERE [numero ordine cliente] IS NOT NULL
);
当然,您也必须对此部分应用相同的方法。例如,如果您在查询中还需要一个UNION ALL
:
SELECT [Ordine numero] AS ordine, [data ordine] AS data, comm AS commessa
FROM [archivio globale]
WHERE [ordine numero] IS NOT NULL;
UNION ALL (
SELECT [numero ordine cliente] AS ordine, [data ordine cliente] AS data, numero AS commessa
FROM [ricambi]
WHERE [numero ordine cliente] IS NOT NULL;
UNION ALL (
SELECT [numero ordine cliente] AS ordine, [data ordine cliente] AS data, numero AS commessa
FROM [trasferte]
WHERE [numero ordine cliente] IS NOT NULL;
UNION ALL
SELECT [numero ordine cliente] AS ordine, [data ordine cliente] AS data, numero AS commessa
FROM [xxx]
WHERE [numero ordine cliente] IS NOT NULL
));
答案 2 :(得分:0)
在我的情况下,MySQL链接到MDB的表也给了我同样的错误。 经过多个小时的搜索和尝试,我的解决方案是在第一列的前面添加“&”,如下所示: 选择''&COUNT(*)作为MYNR,...
这样,我设法使用了UNION ALL 4x。
希望它对某人有帮助