python中的mysql数据库LIKE操作符详解
LIKE 操作符⽤于在 WHERE ⼦句中搜索列中的指定模式。
语法:
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
pattern这⾥就是放指定模板的地⽅,⽽这⾥就要⽤到“ % ”,也叫做通配符
%如果是放在条件前⾯,那就是查以...结尾的数据;例如:%李
%如果是放在条件后⾯,那就是查以...开头的数据;例如:李%
%如果是在条件前后都存在,那就是查包含的数据;例如:%李%
⼩知识点:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%z' at line 1
1064的错误就是LIKE查询时(语法错误),通配符处没加引号,所以才会报错...
正确展⽰例如:"%李%"
⽰例1:终端运⾏sql且WHERE⼦句中使⽤LIKE
查询地址以Hang开头的⼈员信息
root@7c6316b19d80:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 140
Server version: 5.6.51 MySQL Community Server (GPL)
mysql> mysql> select * from test_user where address like 'Hang%';
+----+--------+-------------+----------+
| id | name | mobile | address |
+----+--------+-------------+----------+
| 3 | python | 188******** | Hangzhou |
| 4 | java | 177******** | Hangzhou |
| 5 | php | 155******** | Hangzhou |
| 6 | c# | 177******** | Hangzhou |
+----+--------+-------------+----------+
4 rows in set (0.00 sec)
mysql>
查询地址以u结尾的⼈员信息
mysql> select * from test_user where address like '%u';
+----+--------+-------------+----------+
| id | name | mobile | address |
+----+--------+-------------+----------+
| 3 | python | 188******** | Hangzhou |
| 4 | java | 177******** | Hangzhou |
| 5 | php | 155******** | Hangzhou |
| 6 | c# | 177******** | Hangzhou |
+----+--------+-------------+----------+
4 rows in set (0.00 sec)
mysql>
⽰例2:使⽤python脚本执⾏含LIKE的sql语句
查询地址包含z字符的⼈员信息
import pymysql
# 连接数据库
connection = t(host="localhost", user="root", password="123456",
database="testing", port=3306, charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection:
with connection.cursor() as cursor:
sql = """
SELECT
*
FROM
test_user
WHERE
address LIKE '%z%';
"""
result = cursor.fetchall()
for i in result:
print(i)
MySQLError as _error:
raise _error
{'id': 3, 'name': 'python', 'mobile': '188********', 'address': 'Hangzhou'}
{'id': 4, 'name': 'java', 'mobile': '177********', 'address': 'Hangzhou'}
{'id': 5, 'name': 'php', 'mobile': '155********', 'address': 'Hangzhou'}
{'id': 6, 'name': 'c#', 'mobile': '177********', 'address': 'Hangzhou'}
Process finished with exit code 0
查询地址不包含z字符的⼈员信息
try:
with connection:
with connection.cursor() as cursor:
sql = """
SELECT
*
FROM
test_user
WHERE
address NOT LIKE '%z%';
"""
result = cursor.fetchall()
for i in result:
print(i)
MySQLError as _error:
raise _error
{'id': 1, 'name': '张三三', 'mobile': '177********', 'address': '浙江杭州'}
{'id': 9, 'name': '111', 'mobile': '188********', 'address': '浙江杭州'}
Process finished with exit code 0
⾄此,使⽤LIKE操作符查询完毕...
php远程连接mysql数据库知识点扩展:python中的mysql数据库like模糊查询
%在python中是个特殊的符号,如%s,%d分别代表了字符串占位符和数字占位符。⼤家知道,mysql的模糊查询也需要⽤到%。
所以,可以先把需要查的字符串抽出来,再以参数⽅式传⼊。
args = '%'+subtitle+'%'
sqlQueryTitle="select count(*) from tbl_peng_article where title like '%s'"%args
到此这篇关于python中的mysql数据库LIKE操作符详解的⽂章就介绍到这了,更多相关python mysql like操作符内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论