如何解决显示正确输出的问题?

2024-10-01 17:21:29 发布

您现在位置:Python中文网/ 问答频道 /正文

import mysql.connector
from tabulate 
import tabulate

cnx = mysql.connector.connect(user = 'root' , password = '',host = 'localhost', database = 'karmand')

c = cnx.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS employee (
            Name text,
            Weight integer,
            Height integer
            )''')

c.execute('SELECT * FROM employee ORDER BY Height DESC ')
print(tabulate(c.fetchall()))
cnx.close()

输出错误

--------  --  ---
Mahdi     90  190
Amin      75  180
Mohammad  75  175
Ahmad     60  175
--------  --  ---

输出真值:

--------  --  ---
Mahdi     90  190
Amin      75  180
Ahmad     60  175
Mohammad  75  175
--------  --  ---

如果高度相等,如何编写一个sql命令,根据项目的高度对项目进行排序


Tags: 项目importexecuteconnector高度mysqlemployeeinteger
1条回答
网友
1楼 · 发布于 2024-10-01 17:21:29

ORDERBY子句可以非常简单地包含多个项。因此,要按身高和体重订购,请执行以下操作:

SELECT 
  * 
FROM 
  employee 
ORDER BY 
  Height DESC,
  Weight ASC

现场演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=7f33f82b79724920be1e1499c4dff9da

通过多个字段进行排序的示例也可以在网上的许多地方找到,包括official MySQL documentation

相关问题 更多 >

    热门问题