这篇文章主要介绍“python中的MySQL数据库LIKE操作符怎么用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python中的mysql数据库LIKE操作符怎么用”文章能帮助大家解决问题。
创新互联建站是一家网站设计公司,集创意、互联网应用、软件技术为一体的创意网站建设服务商,主营产品:自适应网站建设、成都品牌网站建设、全网整合营销推广。我们专注企业品牌在网站中的整体树立,网络互动的体验,以及在手机等移动端的优质呈现。成都网站建设、成都网站设计、移动互联产品、网络运营、VI设计、云产品.运维为核心业务。为用户提供一站式解决方案,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏析悦目的作品,网站的价值服务。
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 | 18856565858 | Hangzhou | | 4 | java | 17756565858 | Hangzhou | | 5 | php | 15556565858 | Hangzhou | | 6 | c# | 17748484142 | 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 | 18856565858 | Hangzhou | | 4 | java | 17756565858 | Hangzhou | | 5 | php | 15556565858 | Hangzhou | | 6 | c# | 17748484142 | Hangzhou | +----+--------+-------------+----------+ 4 rows in set (0.00 sec) mysql>
示例2:使用python脚本执行含LIKE的sql语句
查询地址包含z字符的人员信息
import pymysql # 连接数据库 connection = pymysql.connect(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%"; """ cursor.execute(sql) result = cursor.fetchall() for i in result: print(i) except pymysql.err.MySQLError as _error: raise _error
{"id": 3, "name": "python", "mobile": "18856565858", "address": "Hangzhou"} {"id": 4, "name": "java", "mobile": "17756565858", "address": "Hangzhou"} {"id": 5, "name": "php", "mobile": "15556565858", "address": "Hangzhou"} {"id": 6, "name": "c#", "mobile": "17748484142", "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%"; """ cursor.execute(sql) result = cursor.fetchall() for i in result: print(i) except pymysql.err.MySQLError as _error: raise _error
{"id": 1, "name": "张三三", "mobile": "17748484141", "address": "浙江杭州"} {"id": 9, "name": "111", "mobile": "18847474549", "address": "浙江杭州"} Process finished with exit code 0
知识点扩展:python中的mysql数据库like模糊查询
%在python中是个特殊的符号,如%s,%d分别代表了字符串占位符和数字占位符。
大家知道,mysql的模糊查询也需要用到%。
所以,可以先把需要查的字符串抽出来,再以参数方式传入。
args = "%"+subtitle+"%" sqlQueryTitle="select count(*) from tbl_peng_article where title like "%s""%args
关于“python中的mysql数据库LIKE操作符怎么用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。