codingstreets
Search
Close this search box.

Get Started: Python MySQL Limit Table

python-mysql-limit-table
Python MySQL Limit Table - The article concerns the Python MySQL Limit Table that explains how to get the limit number of records using the "LIMIT" statement.

In This Article, You Will Know About Python MySQL Limit Table.

Python MySQL Limit Table – Before moving ahead, let’s know a bit about Python MySQL Update Table.

Table of Contents

Limit the Result

To limit the number of values returned from Table, use “LIMIT” statement.

Example: Return the 3 first row’s values from the table.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT * FROM STU_INFORMATION LIMIT 3")

for result in database_for_mysql:
   print(result)

				
			
python-mysql-limit-table

As a result, it returned the result for 3 first rows.

Example: Return 3 first row’s values from only “name” column.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT name FROM STU_INFORMATION LIMIT 3")

for result in database_for_mysql:
   print(result)

				
			
python-mysql-limit-table

As a result, it returned 3 first row’s values only from “name” column.

Start From Another Position

If you want to skip particular number of values and start from next one, use “OFFSET” statement.

E.g., Returning row’s values from 3 row. Means skipped first 2 row’s values.

Example: Skip 1st row and return row’s value from 2nd row.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT * FROM STU_INFORMATION LIMIT 3 OFFSET 1")

for result in database_for_mysql:
   print(result)

				
			
python-mysql-limit-table

As a result, it skipped the 1st row and returned the result from 2nd row.

Example: Return only 3rd row’s value.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT * FROM STU_INFORMATION LIMIT 1 OFFSET 2")

for result in database_for_mysql:
   print(result)

				
			

Example: Return row’s values from 3rd row.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT * FROM STU_INFORMATION LIMIT 2 OFFSET 2")

for result in database_for_mysql:
   print(result)

				
			

Example: Return all row’s values from 2nd row.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT * FROM STU_INFORMATION LIMIT 3 OFFSET 1")

for result in database_for_mysql:
   print(result)



				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Articles