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
-------- -- ---
输出true:
-------- -- ---
Mahdi 90 190
Amin 75 180
Ahmad 60 175
Mohammad 75 175
-------- -- ---
如果height相等,我如何编写sql命令来根据它们的高度、高和低体重对项目进行排序?
发布于 2020-10-29 14:51:33
Order By子句可以非常简单地包含多个项。因此,要先按身高排序,然后按体重排序,请执行以下操作:
SELECT
*
FROM
employee
ORDER BY
Height DESC,
Weight ASC
现场演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=7f33f82b79724920be1e1499c4dff9da
通过多个字段排序的示例也可以在许多在线位置找到,包括在official MySQL documentation中。
https://stackoverflow.com/questions/64593474
复制相似问题