mysql - 选择 2 列并合并数据
问题描述
我想将 col1 中的相同值与 col2 中的特定数据合并,并将其显示在 html 表中。我的 sql 表看起来像这样
+------+------+
|col1 |col2 |
+------+------+
|1 |test |
+------+------+
|2 |test1 |
+------+------+
|1 |test2 |
+------+------+
|1 |test4 |
+------+------+
|2 |test8 |
+------+------+
预期结果显示在 html 表格中,如下所示。
+------+------+
| |test |
| | |
| 1 |test1 |
| | |
| |test2 |
+------+------+
| |test4 |
| 2 | |
| |test8 |
+------+------+
解决方案
您的示例数据不明确,但您可能正在寻找 GROUP_CONCAT:
SELECT
col1,
GROUP_CONCAT(col2 SEPARATOR '\n') col2
FROM mytable
GROUP BY col1
ORDER BY col1
推荐阅读
- ruby-on-rails - 从字符串到文本的change_column上的Rails ActiveRecord SQlite3数据类型不匹配错误
- javascript - 使用arcgis API在esri地图中查找是否在线
- sql - 试图弄清楚 SQL Server 中的表是如何被填充的
- javascript - 基于组件拆分 Angular 应用程序的主 JS 包
- c - AVR C 为什么不会中断?
- postgresql - Postgres:使用转储将 2 个数据库合并到另一个数据库中
- php - docker中的VisualStudio Code PHP executablePath
- neo4j - 在没有 WHERE 的情况下查找属性不是特定值的节点
- django - Django group by 添加不存在的选项
- css - 为什么 Ionic 4 在 iOS 10 不支持时使用 CSS 属性“包含”?