价值不断重复

时间:2019-09-25 22:02:38

标签: mysql sql

我的SQL看起来像这样:

SELECT GROUP_CONCAT(technologies.tech) AS technologies, personal.city, GROUP_CONCAT(languages.language) AS languages 
FROM personal 
INNER JOIN technologies ON technologies.uid = personal.uid 
INNER JOIN languages ON languages.uid = personal.uid 
WHERE personal.uid = 88

结果:

| technologies        |  city          |  languages               |
|---------------------|:--------------:|-------------------------:|
| PHP,HTML,JavaScript |  Chicago       |  English,English,English |

您看到English不断重复。如何使它只出现一次?

1 个答案:

答案 0 :(得分:1)

您有一个聚合查询,其中SELECT中有未聚合的列,而没有GROUP BY。我建议:

SELECT GROUP_CONCAT(DISTINCTt.tech) AS technologies, p.city, 
       GROUP_CONCAT(DISTINCT l.language) AS languages 
FROM personal p INNER JOIN
     technologies t
     ON t.uid = p.uid INNER JOIN
     languages l
     ON l.uid = p.uid 
WHERE p.uid = 88
GROUP BY p.city;

注意:

  • GROUP_CONCAT(DISTINCT)回答您的问题
  • 表别名使查询更易于阅读。
  • p.city放在SELECT中。 MySQL的最新版本会抱怨。所以我添加了GROUP BY