从sql打印最常见的值

时间:2018-04-30 02:57:33

标签: python sql sqlite count

我在SQLite数据库中有一个单词列表,我想获得最常见的值并将其保存在变量中。我正在使用python3 这是我如何获得最常见的价值。

SELECT emotion,
     COUNT(emotion) AS value_occurrence 
FROM     chatlog
GROUP BY emotion
ORDER BY value_occurrence DESC
LIMIT    1;

1 个答案:

答案 0 :(得分:0)

可能是这样的吗?

#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('yourdb')

cur = conn.cursor()
cur.execute('''SELECT emotion,
     COUNT(emotion) AS value_occurrence 
     FROM   chatlog
     GROUP BY emotion
     ORDER BY value_occurrence DESC
     LIMIT    1''')

rows = cur.fetchall()

    for row in rows:
        x = row[0]
        y = row[1]
        print(x,y)