Mysql
 sql >> Base de données >  >> RDS >> Mysql

La requête UNION multiple ne fonctionne pas

Vous avez marqué votre question comme MySQL et vous utilisez des crochets [] . Autant que je sache, les crochets ne sont pas valables pour MySQL et ne sont valables que pour les produits Microsoft (SQL Server/MS Access). Donc, si vous devez joindre le nom de la table/colonne, utilisez des backticks ` .

D'après la documentation :

Je pense donc que votre requête devrait être :

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, si vous utilisez MS Access, vous devrez utiliser les crochets :

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];