where clause का use करके database से हम filtered data select कर सकते हैं। where clause के किये भी बाकी procedure तो select records की तरह शामे ही रहेगा बस query में where clause use करना पड़ता है।

Python MySQL Where Example
Copy Fullscreen Close Fullscreen
# import mysql module's connector class. 
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = '',
  database = 'python_db'
)
db_cursor = mydb.cursor()
# select record where id = 1.
query = "SELECT * FROM users WHERE id = %s"
where_value = (1, )
db_cursor.execute(query, where_value)
rows = db_cursor.fetchall()
for row in rows : print(row)
Output
C:\Users\Rahulkumar\Desktop\python>python mysql_where.py
(1, 'Rahul', 'Kumar', 'myemail@gmail.com')

where clause के साथ भी fetchall() method से multiple records आएंगे और fetchone() method के साथ सिर्फ single record आएगा।


हालाँकि आप बाकी MySQL query की तरह where clause में भी direct value दे सकते हैं।
For Example -

query = "SELECT * FROM users WHERE last_name='kumar'"
db_cursor.execute(query)
# get all matched record.
rows = db_cursor.fetchall()
for row in rows : print(row)

Output :
(1, 'Rahul', 'Kumar', 'myemail@gmail.com')
(2, 'Mohit', 'Kumar', 'xyz@gmail.com')
(4, 'Girish', 'Kumar', None)

Note* जहाँ तक हो सके query में direct values रखकर where clause न करें , इससे SQL Injection के chances बढ़ जाते हैं। इसलिए हमेशा पहला तरीका ही use करें।

Python Where AND

ऊपर दिए गए examples में आप सिर्फ एक ही condition apply कर रहे थे , multiple condition apply करने के लिए MySQL में AND statement का use किया जाता है।


यह Logical Operators की तरह ही work करता है। Tables से वही records निकल कर आएंगे जो सभी conditions को satisfy करेंगे।
See Example -

Copy Fullscreen Close Fullscreen
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = '',
  database = 'python_db'
)
db_cursor = mydb.cursor()
# select record where first_name='Mohan' AND last_name = 'Singh'.
query = "SELECT * FROM users WHERE first_name = %s AND last_name = %s"
where_values = ('Mohan', 'Singh' ) 
db_cursor.execute(query, where_values)
# get only first matched record.
row = db_cursor.fetchone()
Output
C:\Users\Rahulkumar\Desktop\python>python mysql_where.py
(1, 'Rahul', 'Kumar', 'myemail@gmail.com')

Python MySQL Where OR

OR statement multiple conditions में से कोई एक condition से record match करने पर records select करता है। यह भी Logical Operators की तरह ही work करता है।
Example -

Copy Fullscreen Close Fullscreen
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = '',
  database = 'python_db'
)
db_cursor = mydb.cursor()
# select record where last_name='Singh' OR last_name='Kumar'.
query = "SELECT * FROM users WHERE last_name = %s OR last_name = %s"
where_values = ('Singh', 'Kumar') 
db_cursor.execute(query, where_values)
# get all matched record.
rows = db_cursor.fetchall()
for row in rows : print(row)
Output
C:\Users\Rahulkumar\Desktop\python>python mysql_where.py
(1, 'Rahul', 'Kumar', 'myemail@gmail.com')
(2, 'Mohit', 'Kumar', 'xyz@gmail.com')
(4, 'Girish', 'Kumar', None)
(6, 'Mohan', 'Singh', 'mohan1232@email.com')
(7, 'Sohan', 'singh', 'sihan1321@email.com')

Condition के according सिर्फ वही records select हुए हैं जिन records का last_name Singh / Kumar था।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers