【问题标题】:How to properly collapse MySQL rows into a single list如何正确地将 MySQL 行折叠到单个列表中
【发布时间】:2014-06-19 15:38:09
【问题描述】:

我有一个表格查询

SELECT pub.id, topic.id, pub.title, etc... 
FROM pub, topic, link
WHERE (matching logic)

以形式返回

pub.id  |  topic.id  |  pub.title
---------------------------------
x1      |  a         |  y1
x1      |  b         |  y1
x1      |  ...       |  y1

x2      |  c         |  y2
x2      |  d         |  y2
x2      |  ...       |  y2

我真正想要的是表单的响应

pub.id  |  topic.id  |  pub.title
---------------------------------
x1      |  a, b ...  |  y1
x2      |  c, d ...  |  y2

(我觉得匹配逻辑不重要,有需要我会贴出来的。)

我试过group_concat()coalesce,但没有任何效果。有什么帮助吗?

【问题讨论】:

  • 你是如何尝试的group_concat你能发布那个查询吗?

标签: mysql database coalesce sql-server-group-concat


【解决方案1】:

您是否记得在您的GROUP_CONCAT 测试中包含GROUP BY
如果不对数据进行分组,则无法连接组。

这是一个工作示例:http://sqlfiddle.com/#!2/5cd63/2

应该等于:

SELECT pub.id, GROUP_CONCAT(topic.id SEPARATOR ','), pub.title
FROM pub, topic
WHERE (matching logic)
GROUP BY pub.id, pub.title

【讨论】:

  • 这太完美了,谢谢!当然,我完全忘记了GROUP BY
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 2014-01-12
  • 1970-01-01
相关资源
最近更新 更多