有一个更好的方法吗?

时间:2011-03-22 12:27:06

标签: python sqlite

我有一个包含'cellid''date'(日期时间)和'block'(真实)列的表格。

我正在尝试查找所有已阻止的ID>过去3天1次。

我目前正在这样做:

cells = npar.db.execute("SELECT cellid FROM data WHERE block > 1 AND date=?", (files[0][1],)).fetchall()
cells1 = npar.db.execute("SELECT cellid FROM data WHERE block >1 AND date = ?", (files[1][1],)).fetchall()
self.common(cells,cells1)
cells1 = npar.db.execute("SELECT cellid FROM data WHERE block >1 AND date = ?", (files[2][1],)).fetchall()
self.common(cells,cells1)

print len(cells)
def common(self,a,b):
    for item in a:
            if item not in b:
                a.remove(item)

b = npar.db.execute("SELECT cellid FROM data WHERE block > 1 AND date >= ? GROUP BY cellid HAVING COUNT(cellid) = 3", (files[2][1],)).fetchall()
        print len(b)

len(b)不等于len(单元格)

1 个答案:

答案 0 :(得分:3)

“过去3天”?那以下可能有用吗?

cells = npar.db.execute("SELECT cellid FROM data "
                        "WHERE block > 1 "
                        "AND date >= ? "
                        "GROUP BY cellid "
                        "HAVING COUNT(*) = 3", (files[0][1],)).fetchall()