📜  Python MySQL – BETWEEN 和 IN 运算符

📅  最后修改于: 2022-05-13 01:55:19.157000             🧑  作者: Mango

Python MySQL – BETWEEN 和 IN 运算符

在本文中,我们将使用Python来查看 MySQL 中的数据库操作 BETWEEN 和 IN运算符。

这两个运算符都与 WHERE 查询一起使用,以检查表达式是否在 SQL 中的范围内或值列表中。我们将使用下表来执行各种操作:

SQL BETWEEN条件用于测试表达式是否在值范围内(包括)。这些值包括文本、日期或数字。

  1. 它可以与 SELECT、INSERT、UPDATE 或 DELETE 语句一起使用。
  2. SQL BETWEEN Condition 将返回表达式在 value1 和 value2 范围内的记录。

句法:



例 1: Python MySQL 程序演示 BETWEEN运算符的使用。

Python3
# Import mysql.connector module
import mysql.connector
 
# give connection with xampp
database = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="gfg"
)
 
# Creating cursor object
cur_object = database.cursor()
print("between operator demo")
 
#  query
find = "SELECT cus_id,purchase from geeks where\
purchase between 10 and 100 "
 
# Execute the query
cur_object.execute(find)
 
# fetching results
data = cur_object.fetchall()
print(" ")
print("Purchase between 10 to 100 ")
print(" ")
 
for res in data:
    print(res[0], res[1], sep="--")
print(" ")
 
# Close database connection
database.close()


Python3
# Import mysql.connector module
import mysql.connector
 
# give connection with xampp
database = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="gfg"
)
 
# Creating cursor object
cur_object = database.cursor()
 
#  query
print("in operator demo")
find = "SELECT cus_id,purchase from geeks where \
purchase in (67) "
 
# Execute the query
cur_object.execute(find)
 
# fetching all results
data = cur_object.fetchall()
print("customer id with purchase 67: ")
print(" ")
 
for res in data:
    print(res[0], res[1], sep="--")
print(" ")
print("not in operator demo")
print(" ")
 
find = "SELECT cus_id,purchase from geeks where purchase\
not in (67) "
 
# Execute the query
cur_object.execute(find)
 
# fetching all results
data = cur_object.fetchall()
print(" ")
print("customer id with purchase except 67 purchase ")
print(" ")
 
for res in data:
    print(res[0], res[1], sep="--")
 
# Close database connection
database.close()


输出:

SQL IN运算符允许您轻松测试表达式是否与值列表中的任何值匹配。它用于在 SELECT、INSERT、UPDATE 或 DELETE 中消除对多个 OR 条件的需要。您还可以使用 NOT IN 排除列表中的行。



句法:

例2: Python MySQL程序演示IN运算符的使用。

蟒蛇3

# Import mysql.connector module
import mysql.connector
 
# give connection with xampp
database = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="gfg"
)
 
# Creating cursor object
cur_object = database.cursor()
 
#  query
print("in operator demo")
find = "SELECT cus_id,purchase from geeks where \
purchase in (67) "
 
# Execute the query
cur_object.execute(find)
 
# fetching all results
data = cur_object.fetchall()
print("customer id with purchase 67: ")
print(" ")
 
for res in data:
    print(res[0], res[1], sep="--")
print(" ")
print("not in operator demo")
print(" ")
 
find = "SELECT cus_id,purchase from geeks where purchase\
not in (67) "
 
# Execute the query
cur_object.execute(find)
 
# fetching all results
data = cur_object.fetchall()
print(" ")
print("customer id with purchase except 67 purchase ")
print(" ")
 
for res in data:
    print(res[0], res[1], sep="--")
 
# Close database connection
database.close()

输出: