如何折叠 SQLite 中的结果集?

How do I collapse a result set in SQLite?

假设我有 table

id name
1  nora
2  mars
3  ven

我用这个 table、

加入了 id
id type     value
1  clothing shirt
1  clothing pants
1  toys     abacus
1  toys     legos
      ...

如何制作看起来像

的东西
id name   clothing       toys
1  nora   shirt, pants   abacus, legos 

如果您想将不同的值放入不同的结果列中,简单的连接就无济于事。 您需要使用相关子查询:

SELECT id,
       name,
       (SELECT group_concat(value, ', ')
        FROM Table2
        WHERE id = Table1.id
          AND type = 'clothing'
       ) AS clothing,
       (SELECT group_concat(value, ', ')
        FROM Table2
        WHERE id = Table1.id
          AND type = 'toys'
       ) AS toys
FROM Table1