当前位置:Gxlcms > 数据库问题 > Python MySQL Limit

Python MySQL Limit

时间:2021-07-01 10:21:17 帮助过:4人阅读


章节

  • Python MySQL 入门
  • Python MySQL 创建数据库
  • Python MySQL 创建表
  • Python MySQL 插入表
  • Python MySQL Select
  • Python MySQL Where
  • Python MySQL Order By
  • Python MySQL Delete
  • Python MySQL 删除表
  • Python MySQL Update
  • Python MySQL Limit
  • Python MySQL Join

限制结果数量

可以使用“LIMIT”语句,限制查询返回的记录数量:

示例

在“customers”表中,选择前5条记录:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="你的用户名",
  passwd="你的密码",
  database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers LIMIT 5")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

从指定位置开始

如果想返回,从第3条记录开始的5条记录,可以使用“OFFSET”关键字:

示例

从位置3开始,返回5条记录:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="你的用户名",
  passwd="你的密码",
  database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers LIMIT 5 OFFSET 2")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

Python MySQL Limit

标签:cal   数据   limit   开始   ocs   strong   update   code   custom   

人气教程排行