我有一个数据库,其 ER 图如下所示:
我很难获得所有拥有超过五条消息的用户。我能够用 python 编写查询:
q = "SELECT user_id from users"
for row in cursor.execute(q):
inner_cur = connect.cursor()
n = inner_cur.execute("SELECT count(*) from messages WHERE user_id='{}' ".format(row[0])).fetchall()[0][0]
# print("user_id =", row[0], "num of messages =", n)
if n > 5:
c = connect.cursor()
print(c.execute("SELECT username from users WHERE user_id='{}' ".format(row[0])).fetchall()[0][0])
connect.commit()
但我需要在 DBeaver 中执行此操作。我想也许我需要使用join
,count
或group by
,但我想不出如何使用。有人可以解释一下如何做吗?