sqlite-返回所有列,最多一列,无重复 [英] sqlite - return all columns for max of one column without repeats

查看:66
本文介绍了sqlite-返回所有列,最多一列,无重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python查询SQL数据库.我对数据库还很陌生.我尝试查找此问题,但是找不到足够类似的问题来找到正确的答案.

Im using Python to query a SQL database. I'm fairly new with databases. I've tried looking up this question, but I can't find a similar enough question to get the right answer.

我有一个包含多列/行的表.我想找到单个列的MAX,我希望返回所有列(整个ROW),并且我只想要MAX的一个实例.现在,我得到了十个ROWS,因为MAX重复了十次.我只希望返回一个ROW.

I have a table with multiple columns/rows. I want to find the MAX of a single column, I want ALL columns returned (the entire ROW), and I want only one instance of the MAX. Right now I'm getting ten ROWS returned, because the MAX is repeated ten times. I only want one ROW returned.

到目前为止我尝试过的查询字符串:

The query strings I've tried so far:

sql = 'select max(f) from cbar'  
# this returns one ROW, but only a single COLUMN (a single value)

sql = 'select * from cbar where f = (select max(f) from cbar)'  
# this returns all COLUMNS, but it also returns multiple ROWS

我尝试了很多,但是他们什么也没返回.他们不对劲.这就是问题所在,我太新了,无法在两个有效的查询语句之间找到中间点.

I've tried a bunch more, but they returned nothing. They weren't right somehow. That's the problem, I'm too new to find the middle ground between my two working query statements.

推荐答案

在SQLite 3.7.11或更高版本中,您可以检索所有列以及最大值:

In SQLite 3.7.11 or later, you can just retrieve all columns together with the maximum value:

SELECT *, max(f) FROM cbar;

但是您的Python可能太旧了.通常,您可以按该列对表格进行排序,然后只读取第一行:

But your Python might be too old. In the general case, you can sort the table by that column, and then just read the first row:

SELECT * FROM cbar ORDER BY f DESC LIMIT 1;

这篇关于sqlite-返回所有列,最多一列,无重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆