【问题标题】:how can you select the first 5 students in a table according to marks如何根据分数选择表格中的前5名学生
【发布时间】:2020-01-18 22:40:43
【问题描述】:
cursor.execute("select name,marks from student order by marks desc limit 5")

cx_Oracle.DatabaseError: ORA-00933: SQL 命令未正确结束

如何解决此错误?还有其他方法可以重述查询吗? 这个查询有什么问题?

【问题讨论】:

  • 嗨,studentsstudent 表名?

标签: sql python-3.x


【解决方案1】:

limit 不是 Oracle 的 SQL 语法的一部分。您可以将fetch first 用于此类用例:

cursor.execute("select name, marks from student order by marks desc fetch first 5 rows only")

【讨论】:

  • cursor.execute("select count(*) from student order by tags desc fetch first 5 rows only") cx_Oracle.DatabaseError: ORA-00933: SQL command not properly end :(((跨度>
【解决方案2】:

在 Oracle 12C 中,您可以使用 fetch。在早期版本中,使用子查询和rownum:

select s.*
from (select s.name, s.marks
      from student s
      order by s.marks desc
     ) s
where rownum <= 5;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 2023-04-08
    • 2019-02-24
    相关资源
    最近更新 更多