首页 > 解决方案 > 如何从列表列表中选择最大出现的字符串?

问题描述

在我的数据库中,我有一个表。

statement= """create table itags(
                tag_id number(10) not null primary key,
                tag_name varchar2(50) 
                )"""
cur.execute(statement)
cur.execute("INSERT INTO itags VALUES(301,'Art')")
cur.execute("INSERT INTO itags VALUES(302,'Science')")
cur.execute("INSERT INTO itags VALUES(303,'Music')")
...
so on and so forth
...

现在我想选择并打印出现频率最高的标签名称。

如果我这样做:

cur.execute("select tag_name from itags")
res= cur.fetchall()
print(res)

我将获得一个包含我存储在该表中的标记名称的元组列表。例如:[(艺术,),(科学,),(音乐,),....]

现在我应该如何从这个元组列表中提取出现次数最多的字符串?此外,SQL 命令会比 python 代码更有帮助吗?

标签: sqlpython-3.xselectoracle11gcx-oracle

解决方案


Oracle 通过以下方式为您完成所有繁重的工作stats_mode

SELECT STATS_MODE(tag_name) FROM itags

推荐阅读