Question

http://sqlfiddle.com/#!2/37dd94/17

If I do SELECT DISTINCT I get the same results as doing just SELECT.

On the query results, you will see two activities that contains the District "Evora". Only one should appear.

Any clue?

Was it helpful?

Solution

How about the following query (SQL FIDDLE):

SELECT GROUP_CONCAT(APA_T.district), 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 
ORDER BY APA_T.district;

The above query will eliminate the extra Faro and Evora.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top