sqlite - 获取 SQLite 中 GROUP_CONCAT 的所有 id
问题描述
表 B
id name tablename
1 abc table1
2 xyz table2
3 abc table1
4 sdf table2
5 dfg table1
询问:
SELECT B.tablename, GROUP_CONCAT(B.id),GROUP_CONCAT(B.name||'-'||cnt)
FROM (
SELECT tablename, id, name, COUNT(*) cnt
FROM B
GROUP BY tablename,name
) B GROUP BY B.tablename
输出:
| tablename | GROUP_CONCAT(B.id) | GROUP_CONCAT(B.nameB||'-'||cnt) |
| ------ | ------------------ | ------------------------------- |
| table1 | 1,5 | abc-2,dfg-1 |
| table2 | 2,4 | xyz-1,sdf-1 |
但是在这里我没有得到完整的 ID,正如您在表 1 的输出中看到的那样。
我想要类似的东西:
| tablename | GROUP_CONCAT(B.id) | GROUP_CONCAT(B.nameB||'-'||cnt) |
| ------ | ------------------ | ------------------------------- |
| table1 | 1,3,5 | abc-2,df-1 |
| table2 | 2,4 | xyz-1,sdf-1 |
请帮我解决这个问题。
解决方案
您还应该在子查询中使用GROUP_CONCAT()
s id
:
SELECT tablename, GROUP_CONCAT(id), GROUP_CONCAT(name||'-'||cnt)
FROM (
SELECT tablename, GROUP_CONCAT(id) id, name, COUNT(*) cnt
FROM B
GROUP BY tablename, name
)
GROUP BY tablename
请参阅演示。
推荐阅读
- javascript - Is it okay to include PKs in json response during an ajax call?
- package - (K)ubuntu 19.04 上缺少 qt 模块脚本
- python - Pandas read_sql 不一致的行为依赖于驱动程序?
- google-bigquery - BigQuery:在标准 SQL 中按当前日期动态选择表?
- node.js - Nodejs Winston 在“createLogger”中使用异步函数
- java - 如何向后读取整数文件
- algorithm - 将重叠的凸多边形合并为单个凹多边形的最佳方法?
- python - 有什么方法可以抑制/归一化/平均化 python pandas 图形中的峰值
- selenium - 使用 Protractor 运行并行测试时每个 Chrome 实例的自定义参数
- javascript - 如何保持多个数组处于反应状态?