2

多个 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 语句)?该怎么办?

4

3 回答 3

1

这是一个 MyODBC 错误,请参见此处的官方条目。直到今天,它仍然开放...

[编辑] 我找到了一个简单的解决方案:myODBC 只会理解带有 ONE UNION(或UNION ALL)的查询。UNION所以你必须用 with包裹右边的部分()。另外,您需要以 结束每个查询;,如下所示:

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
));
于 2013-02-21T13:52:52.763 回答
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];
于 2012-10-09T16:46:18.800 回答
0

在我的情况下,MySQL 链接到 MDB 的表给了我同样的错误。经过几个小时的搜索和尝试,我的解决方案是在第一列前面添加 ''&,如下所示: SELECT ''&COUNT(*) AS MYNR,...

通过这种方式,我设法使用了 UNION ALL 4x。

希望它可以帮助某人

错误的链接

于 2020-06-05T19:27:17.743 回答