- Python MySQL entry
- Python MySQL create database
- Python MySQL create table
- Python MySQL Insert Table
- Python MySQL Select
- Python MySQL Where
- Python MySQL Order By
- Python MySQL Delete
- Python MySQL drop table
- Python MySQL Update
- Python MySQL Limit
- Python MySQL Join
related suggestion
Python MySQL Limit
June 4, 2019
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)
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)