赛捷软件论坛's Archiver

vicky.yu 发表于 2021-6-7 09:32

Python MySQL Order By

结果排序
请使用 ORDER BY 语句按升序或降序对结果进行排序。

ORDER BY 关键字默认按升序对结果进行排序。若要按降序对结果进行排序,请使用 DESC 关键字。

实例
以字符顺序对姓名进行排序,结果:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers ORDER BY name"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
  print(x)
运行实例
降序排序
请使用 DESC 关键字按降序对结果进行排序。

实例
按反转字母顺序对姓名的结果进行排序:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers ORDER BY name DESC"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
  print(x)
运行实例
MySQL Where
MySQL Delete


Python 参考手册
Python 实例
Python 测验


W3School 简体中文版提供的内容仅用于培训和测试,不保证内容的正确性。通过使用本站内容随之而来的风险与本站无关。版权所有,保留一切权利。

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.