Python MySQL จำกัด
- หน้าก่อนหน้า MySQL Update
- หน้าต่อไป MySQL Join
จำกัดผลลัพธ์
คุณสามารถจำกัดจำนวนบันทึกที่กลับคืนจากคำสั่งค้นหาด้วยการใช้คำสั่ง "LIMIT" ได้
ตัวอย่าง
เลือก 5 รายการแรกจากตาราง "customers"
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers LIMIT 5") myresult = mycursor.fetchall() for x in myresult: print(x)
เริ่มจากตำแหน่งอื่น
ถ้าต้องการที่จะกลับคืน 5 รายการจากบันทึกที่สาม คุณสามารถใช้คำสั่ง "OFFSET" ได้
ตัวอย่าง
ส่งผลรายงานจากตำแหน่ง 3 กลับคืน 5 รายการ
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers LIMIT 5 OFFSET 2") myresult = mycursor.fetchall() for x in myresult: print(x)
- หน้าก่อนหน้า MySQL Update
- หน้าต่อไป MySQL Join