我们可以制作一个group_concat的DISTINCT(不同的somefield)吗?

时间:2014-05-08 14:14:23

标签: mysql join subquery correlated-subquery

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

如果我SELECT DISTINCT,我会得到与仅仅SELECT相同的结果。

在查询结果中,您将看到两个包含区" Evora"的活动。 只应出现一个。

有任何线索吗?

1 个答案:

答案 0 :(得分:2)

以下查询(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;

上述查询将消除额外的FaroEvora