Python MySQL Limit

Python MySQL Tutorial

related suggestion

Python MySQL Limit

June 4, 2019

 Feedback

Limit the number of results

You can use the "LIMIT" statement, limit the number of records returned by the query:

Examples

In the "customers" table, select first five records:

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)

copy

Starting at the specified location

If you want to return, five records from the beginning of Article 3 of the recording, you can use the "OFFSET" Keywords:

Examples

3 from the start position, 5 records returned:

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)

copy


Doc navigation

← Python MySQL Update

Python MySQL Join →

Guess you like

Origin blog.csdn.net/matthewwu/article/details/93381154