sql >> Database >  >> RDS >> Mysql

Query die één extra record retourneert. Om het even welke raad op hoe te om het van de vraagresultaten te verwijderen?

Doet het onderstaande (SQL Fiddle ) de resultaten opleveren waarnaar u op zoek bent. Ik heb de vakbond ingepakt, zodat ik kon sorteren op de name veld. Als je het niet op die manier wilt, kun je het verwijderen of sorteren op de DistCon veld in plaats daarvan.

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. SQL Server 2016:Queryresultaten opslaan in een CSV-bestand

  2. Hoe jQuery SlickGrid te gebruiken met PHP / MySQL (servergegevens laden en wijzigingen opslaan)

  3. SQL - INDIEN BESTAAT UPDATE ELSE INSERT Syntaxisfout

  4. FOUT 1698 (28000):Toegang geweigerd voor gebruiker 'root'@'localhost'