search
HomeDatabaseSQLSQL fuzzy query statement

SQL fuzzy query statement

Feb 06, 2020 am 10:50 AM
sqlfuzzy query

SQL fuzzy query statement

SQL fuzzy query statement

The general fuzzy statement syntax is as follows:

SELECT 字段 FROM 表 WHERE 某字段 Like 条件

About the conditions, SQL provides There are four matching modes:

1, %: represents any 0 or more characters. Can match characters of any type and length. In some cases, if it is Chinese, please use two percent signs (%%) to express it.

SELECT * FROM [user] WHERE u_name LIKE '%三%'

will find all the records with "three" in u_name as "Zhang San", "Zhang Cat San", "Three-legged Cat", "Tang Sanzang", etc. In addition, if you need to find records with both "三" and "cat" in u_name, please use the and condition

SELECT * FROM [user] WHERE u_name LIKE '%三%' AND u_name LIKE '%猫%'

If you use

SELECT * FROM [user] WHERE u_name LIKE '%三%猫%'

, you can search for "三级" Cat", but the search for "Zhang Cat San" that meets the conditions cannot be found.

2, _: represents any single character. Matches a single arbitrary character. It is often used to limit the character length of expressions:

 SELECT * FROM [user] WHERE u_name LIKE '_三_'

Only find "Tang Sanzang" so that u_name is three characters and the middle character is "三" ;

SELECT * FROM [user] WHERE u_name LIKE '三__';

Only find "three-legged cat" whose name has three characters and the first character is "三";

3. [ ]: represents one of the characters listed in brackets (similar to a regular expression). Specify a character, string, or range, requiring the matched object to be any one of them.

SELECT * FROM [user] WHERE u_name LIKE '[张李王]三'

will find "Zhang San", "Li San", "Wang San" (instead of "Zhang Li Wang San");

Such as [ ] If there is a series of characters (01234, abcde, etc.), it can be abbreviated as "0-4", "a-e"

SELECT * FROM [user] WHERE u_name LIKE '老[1-9]'

will find "old 1", "old 2",... ..., "老9";

4, [^]: Indicates a single character not listed in brackets. Its value is the same as [], but it requires that the matched object is any character other than the specified character.

SELECT * FROM [user] WHERE u_name LIKE '[^张李王]三'

Will find "Zhao San", "Sun San", etc. whose surnames are not "Zhang", "Li", "Wang";

SELECT * FROM [user] WHERE u_name LIKE '老[^1-4]';

Will exclude "Old 1" to "Old 4" and search for "Old 5", "Old 6",...

5. When the query content contains wildcards

Due to wildcards, our query statements for special characters "%", "_", and "[" cannot be implemented normally. However, the special characters can be queried normally if they are enclosed in "[ ]". Based on this, we write the following function:

function sqlencode(str)
str=replace(str,"[","[[]") '此句一定要在最前
str=replace(str,"_","[_]")
str=replace(str,"%","[%]")
sqlencode=str
end function

Before querying, the string to be checked can be processed by this function.

PHP Chinese website has a large number of free SQL tutorials, everyone is welcome to learn!

The above is the detailed content of SQL fuzzy query statement. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
SQL: The Language of Databases ExplainedSQL: The Language of Databases ExplainedApr 27, 2025 am 12:14 AM

SQL is the core tool for database operations, used to query, operate and manage databases. 1) SQL allows CRUD operations to be performed, including data query, operations, definition and control. 2) The working principle of SQL includes three steps: parsing, optimizing and executing. 3) Basic usages include creating tables, inserting, querying, updating and deleting data. 4) Advanced usage covers JOIN, subquery and window functions. 5) Common errors include syntax, logic and performance issues, which can be debugged through database error information, check query logic and use the EXPLAIN command. 6) Performance optimization tips include creating indexes, avoiding SELECT* and using JOIN.

SQL: How to Overcome the Learning HurdlesSQL: How to Overcome the Learning HurdlesApr 26, 2025 am 12:25 AM

To become an SQL expert, you should master the following strategies: 1. Understand the basic concepts of databases, such as tables, rows, columns, and indexes. 2. Learn the core concepts and working principles of SQL, including parsing, optimization and execution processes. 3. Proficient in basic and advanced SQL operations, such as CRUD, complex queries and window functions. 4. Master debugging skills and use the EXPLAIN command to optimize query performance. 5. Overcome learning challenges through practice, utilizing learning resources, attaching importance to performance optimization and maintaining curiosity.

SQL and Databases: A Perfect PartnershipSQL and Databases: A Perfect PartnershipApr 25, 2025 am 12:04 AM

The relationship between SQL and database is closely integrated, and SQL is a tool for managing and operating databases. 1.SQL is a declarative language used for data definition, operation, query and control. 2. The database engine parses SQL statements and executes query plans. 3. Basic usage includes creating tables, inserting and querying data. 4. Advanced usage involves complex queries and subqueries. 5. Common errors include syntax, logic and performance issues, which can be debugged through syntax checking and EXPLAIN commands. 6. Optimization techniques include using indexes, avoiding full table scanning and optimizing queries.

SQL vs. MySQL: Clarifying the Relationship Between the TwoSQL vs. MySQL: Clarifying the Relationship Between the TwoApr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

The Importance of SQL: Data Management in the Digital AgeThe Importance of SQL: Data Management in the Digital AgeApr 23, 2025 am 12:01 AM

SQL's role in data management is to efficiently process and analyze data through query, insert, update and delete operations. 1.SQL is a declarative language that allows users to talk to databases in a structured way. 2. Usage examples include basic SELECT queries and advanced JOIN operations. 3. Common errors such as forgetting the WHERE clause or misusing JOIN, you can debug through the EXPLAIN command. 4. Performance optimization involves the use of indexes and following best practices such as code readability and maintainability.

Getting Started with SQL: Essential Concepts and SkillsGetting Started with SQL: Essential Concepts and SkillsApr 22, 2025 am 12:01 AM

SQL is a language used to manage and operate relational databases. 1. Create a table: Use CREATETABLE statements, such as CREATETABLEusers(idINTPRIMARYKEY, nameVARCHAR(100), emailVARCHAR(100)); 2. Insert, update, and delete data: Use INSERTINTO, UPDATE, DELETE statements, such as INSERTINTOusers(id, name, email)VALUES(1,'JohnDoe','john@example.com'); 3. Query data: Use SELECT statements, such as SELEC

SQL: The Language, MySQL: The Database Management SystemSQL: The Language, MySQL: The Database Management SystemApr 21, 2025 am 12:05 AM

The relationship between SQL and MySQL is: SQL is a language used to manage and operate databases, while MySQL is a database management system that supports SQL. 1.SQL allows CRUD operations and advanced queries of data. 2.MySQL provides indexing, transactions and locking mechanisms to improve performance and security. 3. Optimizing MySQL performance requires attention to query optimization, database design and monitoring and maintenance.

What SQL Does: Managing and Manipulating DataWhat SQL Does: Managing and Manipulating DataApr 20, 2025 am 12:02 AM

SQL is used for database management and data operations, and its core functions include CRUD operations, complex queries and optimization strategies. 1) CRUD operation: Use INSERTINTO to create data, SELECT reads data, UPDATE updates data, and DELETE deletes data. 2) Complex query: Process complex data through GROUPBY and HAVING clauses. 3) Optimization strategy: Use indexes, avoid full table scanning, optimize JOIN operations and paging queries to improve performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment