📜  Python MySQL – LIKE()运算符

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

Python MySQL – LIKE()运算符

在本文中,我们将讨论使用Python语言在 MySQL 中使用 LIKE运算符。

有时我们可能需要数据库中匹配特定模式的元组。例如,我们可能希望检索元组以字母 'y' 开头,或以 'b' 开头并以 'l' 结尾的所有列,或者甚至更复杂和限制性更强的字符串模式。这就是 LIKE 子句派上用场的地方,通常与 SQL 中的 WHERE 子句结合使用。

有两种通配符用于过滤结果:

  • 百分号 (%):用于匹配零个或多个字符。 (可变长度)
  • 下划线 (_):用于精确匹配一个字符。 (定长)

句法:

以下是使用 LIKE 子句进行模式匹配的规则:

PatternMeaning
‘a%’Match strings which start with ‘a’
‘%a’Match strings with end with ‘a’
‘a%t’Match strings which contain the start with ‘a’ and end with ‘t’.
‘%wow%’Match strings which contain the substring ‘wow’ in them at any position.
‘_wow%’Match strings which contain the substring ‘wow’ in them at the second position.
‘_a%’Match strings which contain ‘a’ at the second position.
‘a_ _%’Match strings which start with ‘a’ and contain at least 2 more characters.

为了使用 LIKE 操作,我们将使用下表:

下面是描述如何在Python MySQL 中使用 LIKE运算符的各种示例。

示例 1:

程序显示在 itdept 表中地址以字母 G 开头的行。



Python3
# import mysql.connector module
import mysql.connector
  
# establish connection
database = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="gfg"
)
  
# creating cursor object
cur_object = database.cursor()
print("like operator address starts with G")
  
#  query
find = "SELECT * from itdept where Address like 'G%' "
  
# execute the query
cur_object.execute(find)
  
# fetching all results
data = cur_object.fetchall()
for i in data:
    print(i[0], i[1], i[2], i[3], sep="--")
  
# Close database connection
database.close()


Python3
# import mysql.connector module
import mysql.connector
  
# establish connection
database = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="gfg"
)
  
# creating cursor object
cur_object = database.cursor()
print("like operator name starts with H and ends with A")
  
#  query
find = "SELECT * from itdept where Name like 'H%A' "
  
# execute the query
cur_object.execute(find)
  
# fetching all results
data = cur_object.fetchall()
for i in data:
    print(i[0], i[1], i[2], i[3], sep="--")
  
# close database connection
database.close()


Python3
# import mysql.connector module
import mysql.connector
  
# establish connection
database = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="gfg"
)
  
# creating cursor object
cur_object = database.cursor()
print("like operator address has three letters only")
  
#  query
find = "SELECT * from itdept where Address like '___' "
  
# execute the query
cur_object.execute(find)
  
# fetching all results
data = cur_object.fetchall()
for i in data:
    print(i[0], i[1], i[2], i[3], sep="--")
  
# close database connection
database.close()


输出:

示例 2:

在这里,我们显示表中名称以字母 H 开头并以字母 A 结尾的所有行。

蟒蛇3

# import mysql.connector module
import mysql.connector
  
# establish connection
database = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="gfg"
)
  
# creating cursor object
cur_object = database.cursor()
print("like operator name starts with H and ends with A")
  
#  query
find = "SELECT * from itdept where Name like 'H%A' "
  
# execute the query
cur_object.execute(find)
  
# fetching all results
data = cur_object.fetchall()
for i in data:
    print(i[0], i[1], i[2], i[3], sep="--")
  
# close database connection
database.close()

输出:

示例 3:

在这个程序中,我们显示表中所有包含三个字母的收件人的行。

蟒蛇3

# import mysql.connector module
import mysql.connector
  
# establish connection
database = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="gfg"
)
  
# creating cursor object
cur_object = database.cursor()
print("like operator address has three letters only")
  
#  query
find = "SELECT * from itdept where Address like '___' "
  
# execute the query
cur_object.execute(find)
  
# fetching all results
data = cur_object.fetchall()
for i in data:
    print(i[0], i[1], i[2], i[3], sep="--")
  
# close database connection
database.close()

输出: