【问题标题】:Sqlite group_concat select with "special needs"Sqlite group_concat 选择“特殊需要”
【发布时间】:2010-12-26 12:35:23
【问题描述】:

我知道如何将 group_concat 与 Sqlite 一起使用,以执行以下操作:

id - f1 - f2 - f3
 1 -  1 -  a - NULL
 2 -  1 -  b - NULL
 3 -  2 -  c - NULL
 4 -  2 -  d - NULL

选择 id, f1, group_concat(f2), f3 从表 按 f1 分组

 result:
 2 -  1 - a,b - NULL
 4 -  2 - c,d - NULL

如您所见,ID 的 1 和 3 被删除,这是预期的行为。 但我需要:

 1 -  1 -  a - a,b
 2 -  1 -  b - a,b
 3 -  2 -  c - c,d
 4 -  2 -  d - c,d

所以,每条记录都返回,另一个字段 (f3) 使用 group_concat 更新

知道如何在 Sqlite 中做到这一点吗?

谢谢

【问题讨论】:

    标签: sql sqlite select group-concat


    【解决方案1】:

    不知道你为什么想要这个,但这里是:

    select 
      outer_t.id
     ,outer_t.f1
     ,outer_t.f2
     ,inline_view.groupfoo
     from t as outer_t 
     left join (
      select 
          f1
         ,group_concat(f2) as groupfoo 
        from t 
        group by f1
     ) inline_view on inline_view.f1 = outer_t.f1
    ;
    

    【讨论】:

      【解决方案2】:

      使用嵌入的 sql 语句

      select id, f1, f2, (select group_concat(f2) from t t2 where t2.f1 = t1.f1)
      from t t1
      

      【讨论】:

        猜你喜欢
        • 2015-06-23
        • 1970-01-01
        • 2011-10-26
        • 2011-02-16
        • 2018-09-23
        • 1970-01-01
        • 1970-01-01
        • 2017-09-03
        • 1970-01-01
        相关资源
        最近更新 更多