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

Detailed explanation of JavaScript data types

Table of contents 1. Literals 1.1 Numeric literal...

Advantages and disadvantages of common MySQL storage engines

Table of contents View all storage engines InnoDB...

Tips for Mixing OR and AND in SQL Statements

Today, there is such a requirement. If the logged...

How to install openssh from source code in centos 7

Environment: CentOS 7.1.1503 Minimum Installation...

A thorough analysis of HTML special characters

A Thorough Analysis of HTML (14) Special Characte...

Troubleshooting MySQL high CPU load issues

High CPU load caused by MySQL This afternoon, I d...

Complete steps to solve 403 forbidden in Nginx

The webpage displays 403 Forbidden Nginx (yum ins...

Detailed explanation of two points to note in vue3: setup

Table of contents In vue2 In vue3 Notes on setup ...

jQuery plugin to implement accordion secondary menu

This article uses a jQuery plug-in to create an a...

Solve the problem of docker images disappearing

1. Mirror images disappear in 50 and 93 [root@h50...

WeChat Mini Program to Implement Electronic Signature

This article shares the specific code for impleme...

Introduction to MyCat, the database middleware

1. Mycat application scenarios Mycat has been dev...

Tutorial on installing MySQL 5.7.9 using RPM package under CentOS 7

Recorded MySQL 5.7.9 installation tutorial, share...