Detailed explanation of the mysql database LIKE operator in python

Detailed explanation of the mysql database LIKE operator in python

The LIKE operator is used in the WHERE clause to search for a specified pattern in a column.

grammar:

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern

Pattern is where the specified template is placed, and here we need to use "%", also known as a wildcard

If % is placed before the condition, it will search for data ending with ...; for example: %李

If % is placed after the condition, it will search for data starting with...; for example: Li%

%If it exists before and after the condition, it means to check the included data; for example: %李%

Tips:

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

Error 1064 is caused by the fact that there are no quotes around the wildcard when performing a LIKE query (syntax error), so an error is reported...

Correct display example: "%李%"

Example 1: The terminal runs SQL and LIKE is used in the WHERE clause

Query information of people whose addresses begin with 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> 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>

Query the information of people whose addresses end with 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>

Example 2: Use Python script to execute SQL statements containing LIKE

Query the information of people whose addresses contain the character z

import pymysql
 
# Connect to the database 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

Query the information of people whose addresses do not contain the character 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': 'Hangzhou, Zhejiang'}
 
Process finished with exit code 0

At this point, the query using the LIKE operator is complete...

picture

Knowledge point extension: MySQL database like fuzzy query in Python

% is a special symbol in Python, such as %s and %d, which represent string placeholders and number placeholders respectively.

As we all know, MySQL's fuzzy query also requires %.

Therefore, you can first extract the string you need to check, and then pass it in as a parameter.

args = '%'+subtitle+'%'
sqlQueryTitle="select count(*) from tbl_peng_article where title like '%s'"%args

This is the end of this article about the detailed explanation of the MySQL database LIKE operator in Python. For more relevant content about the Python MySQL like operator, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Details about the like operator in MySQL

<<:  Detailed explanation of the difference between run/cmd/entrypoint in docker

>>:  CSS style does not work (the most complete solution summary in history)

Recommend

Troubleshooting and solutions for MySQL auto-increment ID oversize problem

introduction Xiao A was writing code, and DBA Xia...

How to update v-for in Vue

Tips: Array change method will cause v-for to upd...

Summary of several error logs about MySQL MHA setup and switching

1: masterha_check_repl replica set error replicat...

RHCE installs Apache and accesses IP with a browser

1. at is configured to write "This is a at t...

How to use dd command in Linux without destroying the disk

Whether you're trying to salvage data from a ...

CSS3 achieves various border effects

Translucent border Result: Implementation code: &...

4 principles for clean and beautiful web design

This article will discuss these 4 principles as t...

How to implement paging query in MySQL

SQL paging query:background In the company's ...

VMware Workstation installation Linux system

From getting started to becoming a novice, the Li...

Detailed explanation of ES6 Promise usage

Table of contents What is a Promise? Usage of rej...

Solution to the failure of docker windows10 shared directory mounting

cause When executing the docker script, an error ...

How to export CSV file with header in mysql

Refer to the official document http://dev.mysql.c...

How to add fields to a large data table in MySQL

Preface I believe everyone is familiar with addin...