취미와 밥줄사이

[Python] MySQL Limit 본문

DB

[Python] MySQL Limit

취미와 밥줄사이 2021. 10. 28. 10:54

Limit the Result


  • "LIMIT"문을 사용하여 쿼리로 부터 반환되는 records수를 제한할 수 있다.
  • # Select the 5 first records in the "customers" table
    
    import mysql.connector
    
    mydb = mysql.connector.connect(
    	host="localhost",
        user="yourusername",
        password="your password",
        database="mydatabase"
        )
    
    mycursor = mydb.cursor()
    
    mycursor.execute("SELECT * FROM customers LIMIT 5")
    
    myresult = mycursor.fetchall()
    
    for x in myresult:
    	print(x)

다른 위치에서 시작


  • 세 번째 records부터 5개의 records를 반환하려면 "OFFSET" 키워드를 사용할 수 있습니다.
  • # Start from position 3, and return 5 records
    
    import mysql.connector
    
    mydb = mysql.connector.connect(
    	host="localhost",
        user="your username",
        password="your password",
        database="mydatabase"
        )
        
    mycursor = mydb.cursor()
    
    mycursor.execute("SELECT * FROM customers LIMIT 5 OFFSET 2"0
    
    myresult = mycursor.fetchall()
    
    for x in myresult:
    	print(x)

REPERENCE


https://www.w3schools.com/python/python_mysql_limit.asp

 

Python MySQL Limit

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

'DB' 카테고리의 다른 글

[Python] MongoDB  (0) 2021.10.28
[Python] MySQL Join  (0) 2021.10.28
[Python] MySQL Update Table  (0) 2021.10.28
[Python] MySQL Drop Table  (0) 2021.10.28
[Python] MySQL Delete From By  (0) 2021.10.28