The multiple UNION query:
SELECT [Ordine numero] AS ordine, [data ordine] AS data, comm AS commessa
FROM [archivio globale]
WHERE [ordine numero] IS NOT NULL
You have tagged your question as MySQL and you are using square brackets []
. As far as I know, square brackets are not valid for MySQL and are only valid for Microsoft products (SQL Server/MS Access). So if you need to enclose table/column name use backticks `
.
From the documentation:
The identifier quote character is the backtick (“`”):
So I think your query should be:
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`;
Edit, if you are using MS Access then you will need to use the square brackets:
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];
It's a MyODBC bug see offical entry here. As of today, it's still open...
[EDIT] I found a simple solution: myODBC will only understand queries with ONE UNION
(or UNION ALL
). So you have to wrap the right part of the UNION
with ()
. Plus, you'll need to end each query with ;
, like this:
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
);
And of course, you must apply the same method to this part, too. For instance, if you needed one more UNION ALL
in you query:
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
));
In my case MySQL linked tables to MDB gave me the same error. After multiple hours of search and try , my solution was to add ''& in front of the first column, like this: SELECT ''&COUNT(*) AS MYNR,...
this way i managed to use UNION ALL 4x.
Hope it helps someone
The link to the bug