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

CentOS 7 set grub password and single user login example code

There are significant differences between centos7...

Installation method of MySQL 5.7.18 decompressed version under Win7x64

Related reading: Solve the problem that the servi...

A brief analysis of the difference between ref and toRef in Vue3

1. ref is copied, the view will be updated If you...

Detailed explanation of the payment function code of the Vue project

1. Alipay method: Alipay method: Click Alipay to ...

Analyzing the troublesome Aborted warning in MySQL through case studies

This article mainly introduces the relevant conte...

Application scenarios and design methods of MySQL table and database sharding

Many friends have asked in forums and message are...

Jmeter connects to the database process diagram

1. Download the MySQL jdbc driver (mysql-connecto...

Example of how to change the line spacing of HTML table

When using HTML tables, we sometimes need to chan...

How to add default time to a field in MySQL

Date type differences and uses MySQL has five dat...

How to redirect URL using nginx rewrite

I often need to change nginx configuration at wor...

MySQL data loss troubleshooting case

Table of contents Preface On-site investigation C...