在SQLite中获取组_CONCAT的所有ID

在SQLite中获取组_CONCAT的所有ID,sqlite,group-concat,Sqlite,Group Concat,表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,

表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
但这里我并没有得到完整的ID,正如您在表1的输出中看到的那个样

我想要像这样的东西:

| 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
s使用
GROUP\u CONCAT()

| 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                     |


查看。

非常感谢。我对这个sqlite查询完全陌生,因为我一直在使用ORM。
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