Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Beberapa kueri UNION tidak berfungsi

Anda telah menandai pertanyaan Anda sebagai MySQL dan Anda menggunakan tanda kurung siku [] . Sejauh yang saya tahu, tanda kurung siku bukan berlaku untuk MySQL dan hanya berlaku untuk produk Microsoft (SQL Server/MS Access). Jadi jika Anda perlu menyertakan nama tabel/kolom gunakan backticks ` .

Dari dokumentasi:

Jadi menurut saya kueri Anda seharusnya:

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

Sunting, jika Anda menggunakan MS Access maka Anda harus menggunakan tanda kurung siku:

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Pilih dan tampilkan hanya catatan duplikat di MySQL

  2. MySQL Trigger tidak dapat memperbarui tabel - mendapatkan ERROR 1442

  3. Bagaimana cara saya menggunakan penghapusan kaskade di mysql?

  4. Meningkatkan kueri menggunakan banyak gabungan dalam ke wp_postmeta, tabel kunci/nilai

  5. Bagaimana cara membuat dua kolom kenaikan otomatis di MySQL?