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

Permintaan mengembalikan satu catatan tambahan. Adakah saran tentang cara menghapusnya dari hasil kueri?

Apakah di bawah ini (SQL Fiddle ) menghasilkan hasil yang Anda cari. Saya membungkus serikat sehingga saya kemudian bisa mengurutkan name bidang. Jika Anda tidak menginginkannya, Anda dapat menghapusnya atau mengurutkannya di DistCon sebagai gantinya.

SELECT * FROM 
(
  SELECT GROUP_CONCAT(APA_T.district) AS DistCon, t.name
  FROM tbl_activity AS t 
  JOIN tbl_activity_package AS ap ON t.id = ap.id_activity 
  JOIN 
  (
    SELECT DISTINCT apa.district AS district, 
    (
       SELECT s1.id_activity_package 
       FROM tbl_activity_package_address s1
       WHERE apa.district = s1.district
       ORDER BY s1.id DESC
       LIMIT 1
    ) AS idActivityPackage
    FROM 
    tbl_activity_package_address apa
    ORDER BY apa.district
  ) AS APA_T
  ON ap.id = APA_T.idActivityPackage
  GROUP BY t.name 
  UNION 
  SELECT GROUP_CONCAT(apa.district), t.name
  FROM tbl_activity AS t 
  JOIN tbl_activity_package AS ap ON t.id = ap.id_activity 
  JOIN tbl_activity_package_address AS apa ON ap.id = apa.id_activity_package
  WHERE t.name NOT IN 
  (
    SELECT DISTINCT t.name
    FROM tbl_activity AS t 
    JOIN tbl_activity_package AS ap ON t.id = ap.id_activity 
    JOIN 
    (
      SELECT DISTINCT apa.district AS district, 
      (
         SELECT s1.id_activity_package 
         FROM tbl_activity_package_address s1
         WHERE apa.district = s1.district
         ORDER BY s1.id DESC
         LIMIT 1
      ) AS idActivityPackage
      FROM 
      tbl_activity_package_address apa
    ) AS APA_T
    ON ap.id = APA_T.idActivityPackage
  )
  GROUP BY t.name 
) AS Mm
ORDER BY  Mm.name


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cara Mengatur Ulang Kata Sandi Root di MySQL 8.0

  2. PHP - Data POST salah dikirim melalui formulir saat gema?

  3. Hapus baris jika tabel ada SQL

  4. Matikan atau hentikan kueri MySQL setelah keluar dari skrip PHP

  5. Masukkan tanggal/waktu saat ini menggunakan now() di bidang menggunakan MySQL/PHP