Have a question?
Message sent Close
View Categories

Python MySQL Limit

< 1 min read

Limit the Result – Үр дүн хязгаарлах #

You can limit the number of records returned from the query, by using the “LIMIT” statement:

“LIMIT”-г ашиглан хүсэлтээс буцсан мөрүүдийн тоог хязгаарлаж болно.

Example – Жишээ #

Select the 5 first records in the “customers” table:

“customers” хүснэгтээс эхний 5 мөрийг сонгох:

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 5")
myresult = mycursor.fetchall()
for x in myresult:
print(x)

Start From Another Position – Өөр байршлаас эхлүүлэх #

If you want to return five records, starting from the third record, you can use the “OFFSET” keyword:

Хэрвээ та 3 дах мөрнөөс эхэлж 5 мөрийг буцааж авахийг хүсвэл, “OFFSET” түлхүүр үгийг ашиглана:

Example – Жишээ #

Start from position 3, and return 5 records:

3 дах байршил дах мөрөөс эхэлж 5 мөрийг буцаа: 

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 5 OFFSET 2")
myresult = mycursor.fetchall()
for x in myresult:
  print(x)

Powered by BetterDocs

Leave a Reply