当我们必须将MySQL与其他编程语言一起使用时, 可以使用连接器。 MySQL-connector的工作是提供对使用所需语言的MySQL驱动程序的访问。因此, 它在编程语言和MySQL Server之间生成了连接。
OrderBy排序
OrderBy用于按升序或降序排列结果集。默认情况下, 除非提到" DESC"(以降序排列), 否则它始终以升序排列。
" ASC"也可以用于按升序明确排列。但是, 通常不会以这种方式进行操作, 因为默认情况下已经这样做了。
句法-
SELECT column1, column2
FROM table_name
ORDER BY column_name ASC|DESC;
以下程序将帮助你更好地理解这一点。
正在使用的数据库:
范例1:程序按名称升序排列数据
# Python program to demonstrate
# order by clause
import mysql.connector
# Connecting to the Database
mydb = mysql.connector.connect(
host = 'localhost' , database = 'College' , user = 'root' , password = ''
)
cs = mydb.cursor()
# Order by clause
statement = "SELECT * FROM Student ORDER BY Name"
cs.execute(statement)
result_set = cs.fetchall()
for x in result_set:
print (x)
# Disconnecting from the database
mydb.close()
输出如下:
范例2:按降序排列数据库
# Python program to demonstrate
# order by clause
import mysql.connector
# Connecting to the Database
mydb = mysql.connector.connect(
host = 'localhost' , database = 'College' , user = 'root' , )
cs = mydb.cursor()
# Order by clause
statement = "SELECT * FROM Student ORDER BY Name DESC"
cs.execute(statement)
result_set = cs.fetchall()
for x in result_set:
print (x)
# Disconnecting from the database
mydb.close()
输出如下:
范例3:从表中获取名称的程序, 按卷号降序排列。
# Python program to demonstrate
# order by clause
import mysql.connector
# Connecting to the Database
mydb = mysql.connector.connect(
host = 'localhost' , database = 'College' , user = 'root' , )
cs = mydb.cursor()
# Order by clause
statement = "SELECT Name FROM Student ORDER BY Roll_no DESC"
cs.execute(statement)
result_set = cs.fetchall()
for x in result_set:
print (x)
# Disconnecting from the database
mydb.close()
输出如下:
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。