Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Mehrere UNION-Abfragen funktionieren nicht

Sie haben Ihre Frage als MySQL getaggt und verwenden eckige Klammern [] . Eckige Klammern sind meines Wissens nicht gelten für MySQL und gelten nur für Microsoft-Produkte (SQL Server/MS Access). Wenn Sie also Tabellen-/Spaltennamen einschließen müssen, verwenden Sie Backticks ` .

Aus der Dokumentation:

Ich denke also, Ihre Abfrage sollte lauten:

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

Bearbeiten, wenn Sie MS Access verwenden, müssen Sie die eckigen Klammern verwenden:

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