python中的mysql資料庫LIKE運算子怎麼用

WBOY
發布: 2023-05-31 21:46:10
轉載
1596 人瀏覽過

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運算子怎麼用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!