我只想在SQLite中对要连接的行多的那些记录执行group-concat。似乎您可以预先进行此操作(使用group来计数记录,然后在继续group_concat之前删除那些单例行);之后(完成group_concat,然后删除未串联的行);甚至在期间?

我的问题:SQLite最快的方法是什么?

我已经在Python中使用APSW制定了一个“之后”示例,但对此不满意:

#set up a table with data
c.execute("create table foo(x,y)")
def getvals():
    a = [1, 1, 2, 3, 3]
    b = ['a','b','c','d','e']
    for i in range(5):
        yield a[i],b[i]
c.executemany("insert into foo values(?,?)",getvals())
c.execute('''create table fooc(a,b);
    insert into fooc(a,b) select x, group_concat(y) from foo group by x''')
c.execute('select * from fooc')
c.fetchall()  ## reports three records
c.execute("select * from fooc where b like '%,%'")
c.fetchall() ## reports two records .. what I want


使用LIKE满足这种需求似乎很疯狂(并且很慢?)。

最佳答案

在查询中添加HAVING子句:

INSERT INTO fooc(a,b)
     SELECT x, group_concat(y)
       FROM foo
   GROUP BY x
     HAVING COUNT(*) > 1

08-19 10:05