![](/img/trans.png)
[英]How do i select the value of a certain column from the last row in sqlite3 and python?
[英]How do I determine the highest value of three columns from the same row, Python, SQLite3
我正在尝试显示在python中创建的基本算术测试的结果,该测试重复了三遍并收集了三个分数,这很好用,但是我尝试按降序显示三个分数中的最高分数显示这些显示错误消息。 如下。
Traceback (most recent call last):
File "F:\Adair,Rowan CA2\Task 3\Code\DisplayTablesScore.py", line 4, in <module>
cursor.execute("SELECT * FROM class1 ORDER BY (score1,score2,score3) DESC")
sqlite3.OperationalError: near ",": syntax error
但是,如果按某一列进行组织(例如仅评分1),则效果很好。 我无法弄清楚如何修复的代码如下。
import sqlite3
connection = sqlite3.connect("class1.db")
cursor = connection.cursor()
cursor.execute("SELECT * FROM class1 ORDER BY (score1,score2,score3) DESC")
print("Class 1:")
result = cursor.fetchall()
for r in result:
print(r)
任何帮助,我们将不胜感激,我也在努力确定平均值。
首先,ORDER BY的语法错误,应为
SELECT * FROM class1 ORDER BY score1 DESC, score2 DESC, score3 DESC;
根据多列Max的响应,如果您需要按列的高分排序,则需要计算它们
SELECT score1, score2, score3,
CASE
WHEN score1 >= score2 AND score1 >= score3 THEN score1
WHEN score2 >= score1 AND score2 >= score3 THEN score2
WHEN score3 >= score1 AND score3 >= score2 THEN score3
ELSE score1
END AS high_score FROM class1 ORDER BY high_score DESC;
SQL子句ORDER BY
在声明其参数时不带括号。 换句话说,执行ORDER BY (...)
确实是无效的语法。
要声明要排序的多列,只需省略括号:
cursor.execute("SELECT * FROM class1 ORDER BY score1 DESC, score2 DESC ,score3 DESC")
此示例应涵盖基础知识。 请注意,每列可能需要其自己的降序/升序关键字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.