Can we make a DISTINCT of a group_concat(distinct somefield)?

我只是一个虾纸丫 提交于 2019-12-08 05:11:40

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23544348/can-we-make-a-distinct-of-a-group-concatdistinct-somefield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!