這篇文章主要介紹“python中的MySQL數(shù)據(jù)庫(kù)LIKE操作符怎么用”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“python中的mysql數(shù)據(jù)庫(kù)LIKE操作符怎么用”文章能幫助大家解決問(wèn)題。
創(chuàng)新互聯(lián)建站是一家網(wǎng)站設(shè)計(jì)公司,集創(chuàng)意、互聯(lián)網(wǎng)應(yīng)用、軟件技術(shù)為一體的創(chuàng)意網(wǎng)站建設(shè)服務(wù)商,主營(yíng)產(chǎn)品:自適應(yīng)網(wǎng)站建設(shè)、成都品牌網(wǎng)站建設(shè)、全網(wǎng)整合營(yíng)銷推廣。我們專注企業(yè)品牌在網(wǎng)站中的整體樹(shù)立,網(wǎng)絡(luò)互動(dòng)的體驗(yàn),以及在手機(jī)等移動(dòng)端的優(yōu)質(zhì)呈現(xiàn)。成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、移動(dòng)互聯(lián)產(chǎn)品、網(wǎng)絡(luò)運(yùn)營(yíng)、VI設(shè)計(jì)、云產(chǎn)品.運(yùn)維為核心業(yè)務(wù)。為用戶提供一站式解決方案,我們深知市場(chǎng)的競(jìng)爭(zhēng)激烈,認(rèn)真對(duì)待每位客戶,為客戶提供賞析悅目的作品,網(wǎng)站的價(jià)值服務(wù)。
LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式。
語(yǔ)法:
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern
pattern這里就是放指定模板的地方,而這里就要用到“ % ”,也叫做通配符
%如果是放在條件前面,那就是查以...結(jié)尾的數(shù)據(jù);例如:%李
%如果是放在條件后面,那就是查以...開(kāi)頭的數(shù)據(jù);例如:李%
%如果是在條件前后都存在,那就是查包含的數(shù)據(jù);例如:%李%
小知識(shí)點(diǎn):
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的錯(cuò)誤就是LIKE查詢時(shí)(語(yǔ)法錯(cuò)誤),通配符處沒(méi)加引號(hào),所以才會(huì)報(bào)錯(cuò)...
正確展示例如:"%李%"
示例1:終端運(yùn)行sql且WHERE子句中使用LIKE
查詢地址以Hang開(kāi)頭的人員信息
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(píng)結(jié)尾的人員信息
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腳本執(zhí)行含LIKE的sql語(yǔ)句
查詢地址包含z字符的人員信息
import pymysql # 連接數(shù)據(jù)庫(kù) 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
知識(shí)點(diǎn)擴(kuò)展:python中的mysql數(shù)據(jù)庫(kù)like模糊查詢
%在python中是個(gè)特殊的符號(hào),如%s,%d分別代表了字符串占位符和數(shù)字占位符。
大家知道,mysql的模糊查詢也需要用到%。
所以,可以先把需要查的字符串抽出來(lái),再以參數(shù)方式傳入。
args = "%"+subtitle+"%" sqlQueryTitle="select count(*) from tbl_peng_article where title like "%s""%args
關(guān)于“python中的mysql數(shù)據(jù)庫(kù)LIKE操作符怎么用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。