力扣:写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名相同,报'Rank from scores s order by score desc'错误

今天在用python连接数据库做力扣题时发现这样一个错误

Traceback (most recent call last):
  File "D:/PYTHON/test/力扣/力扣.py", line 864, in <module>
    cursor.execute(sql)
  File "D:\pythonData\lib\site-packages\MySQLdb\cursors.py", line 209, in execute
    res = self._query(query)
  File "D:\pythonData\lib\site-packages\MySQLdb\cursors.py", line 315, in _query
    db.query(q)
  File "D:\pythonData\lib\site-packages\MySQLdb\connections.py", line 231, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Rank from scores s order by score desc' at line 1")

我的代码是这样得

import MySQLdb
conn=MySQLdb.Connect(host='localhost',user='root',password='123456',db='delete')
cursor=conn.cursor()
sql='select score,(select count(distinct score) from scores where score>=s.score) as Rank from scores s order by score desc;'
# sql='select count(distinct score) as Rank from scores where score'
cursor.execute(sql)
res=cursor.fetchall()
print(res)
cursor.close()
conn.close()

按照正常思路,系统提示得这一句就是问题所在: ‘Rank from scores s order by score desc’,然而当我去掉‘,(select count(distinct score) from scores where score>=s.score) as Rank’这句时,程序会正常运行,那这样得话错的岂不是“Rank”?
当我单独把这句话单独拿出来运行时也会报错,也不知道是什么原因。之后我在网上找也没找到答案,后来抱着尝试得心里把‘as Rank’删除了,程序正常运行。

((4.0, 1), (4.0, 1), (3.85, 2), (3.65, 3), (3.65, 3), (3.5, 4))

Process finished with exit code 0

后来,我在Navicat上也试了试sql语句,也会报错,但不去命名则会正常运行

所以,应该是pycharm等编译器不能命名显示的原因

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穿背心的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值