Home Database Mysql Tutorial Talk about the problem of SQL query fields being included in statements

Talk about the problem of SQL query fields being included in statements

Aug 11, 2017 pm 03:08 PM
Include Field statement

Speaking of SQL fuzzy queries, the first thing that comes to mind should be the like keyword. When we need to query data containing a specific field, we often use the '%keyword%' query method. Please refer to this article for specific code examples

Preface

When it comes to fuzzy queries in SQL, the first thing that comes to mind should be the like keyword.

When we need to query data containing a specific field, we often use the '%keyword%' query method. For example:


SELECT ... FROM 表名 WHERE 字段名 LIKE '%关键字%'
Copy after login

This should be regarded as a typical "contains XXX" method, but what if we need to query data that contains specific characters in a field?

For example, I have a contact data table ConnectName, which has a field for recording names. I want to get the contact information of people named Xiaolan and Haiyuan. Under normal circumstances, the first thing we can think of is:


SELECT * FROM ConnectName 
WHERE
  name = '小兰'
  OR name = '灰原'
Copy after login

This approach can achieve this purpose. If at this time, I suddenly want to check another person, such as "Conan", then we have to modify the SQL structure and add a Where conditional sentence:


SELECT * FROM ConnectName 
WHERE
  name = '小兰'
  OR name = '灰原'
  OR name = '柯南'
Copy after login

We know that the OR condition query itself is inefficient, and the structural change statement is a little more troublesome to implement in MyBatis (of course it can also be implemented, just traverse the inserted fields).

Can it be simpler? Can I put all the keywords together and use only one Where condition to achieve it?

CHARINDEX appears

At this time, we can use the CHARINDEX keyword. CHARINDEX can return a certain field that appears in a string of text The position is similar to the usage of String's indexOf. Without further ado, let's give a chestnut:


CHARINDEX('李白','曹操很帅') =0
Copy after login

In the chestnut above, because Cao Cao is very handsome, the key to Li Bai is not included. word, so it cannot be found, and returns 0.


CHARINDEX('李白','李白很帅') =1
Copy after login

The same chestnut, because it contains the Li Bai keyword, will return the index of the first word where the keyword is located , so return 1.

After understanding the usage, we can use the CHARINDEX keyword to optimize our SQL statement:


SELECT * FROM ConnectName 
WHERE
  CHARINDEX(name ,'小兰灰原柯南')>0
Copy after login

If the name field The corresponding name appears in 'Conan Xiaolan Haibara', then the CHARINDEX function will return greater than 1, and we can get the data we want (the three of them can also play happily together^-^)

The corresponding mybatis implementation is also relatively simple


SELECT * FROM ConnectName 
WHERE
  <!--[CDATA[ AND CHARINDEX(name ,#{传入的参数}) --> 0 ]]>
Copy after login

If we want to add a new person later, such as Mouri Kogoro, we only need to pass in the parameters Just add 'Xiaoran Haibara Conan Mouri Kogoro' to it. Isn't it much simpler?

The above is the detailed content of Talk about the problem of SQL query fields being included in statements. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to determine if a field is empty in PHP? How to determine if a field is empty in PHP? Mar 20, 2024 pm 03:09 PM

PHP is a scripting language widely used in website development. For developers, it is often necessary to determine whether a field is empty. In PHP, determining whether a field is empty can be achieved through some simple methods. This article will introduce how to determine whether a field is empty in PHP, and provide specific code examples for your reference. In PHP, you can usually use the empty() function or isset() function to determine whether a field is empty. Next, we introduce the usage of these two functions respectively. Use the empty() function

NoSuchFieldError in Java - Solution to field not found NoSuchFieldError in Java - Solution to field not found Jun 25, 2023 am 11:33 AM

NoSuchFieldError in Java - Solution to field not found Java is a high-level programming language that is widely used in enterprise-level applications and large-scale data processing. During the development process of Java, errors such as NoSuchFieldError may occur. This error means that the JVM cannot find the required field at runtime. In this article, we will take a deeper look at NoSuchFieldError and how to resolve it. What is NoSuchFieldE

What does mysql field mean? What does mysql field mean? Jul 10, 2023 pm 02:14 PM

A mysql field is a column of a specific type and length in a mysql database table that is used to store data. In MySQL, each field must have a specific data type. Common data types include integers, floating point numbers, strings, dates, and times. These data types determine the data that MySQL can store in each field.

How to add fields to the database table How to add fields to the database table Mar 18, 2021 pm 02:13 PM

Methods to add fields in the table: 1. Use the "ALTER TABLE table name ADD new field name data type;" statement to add fields at the end; 2. Use the "ALTER TABLE table name ADD new field name data type FIRST;" statement to add it at the beginning Field; 3. Use the "ALTER TABLE table name ADD new field name data type [constraints] AFTER existing field name;" statement to add a field in the middle.

multi-catch statement in PHP8.0 multi-catch statement in PHP8.0 May 14, 2023 pm 01:51 PM

With the development of web applications, PHP language has been widely used in web development. In the PHP8.0 version, a new language feature was introduced - the multi-catch statement. What is a multi-catch statement? In previous PHP versions, developers needed to write multiple catch statements to handle multiple exception types. For example, the following code block shows the handling of two different exceptions: try{//Somecodethatmay

How to tell if a string contains specific characters using JavaScript? How to tell if a string contains specific characters using JavaScript? Oct 21, 2023 am 08:23 AM

How to tell if a string contains specific characters using JavaScript? In JavaScript, we can use several methods to determine whether a string contains specific characters. Three commonly used methods will be introduced below and corresponding code examples will be given. Method 1: Use the indexOf() method You can use the indexOf() method of the string to determine whether a string contains the specified characters. It returns the index of the first occurrence of a specific character in a string, such as

How to implement the statement to unlock the table in MySQL? How to implement the statement to unlock the table in MySQL? Nov 08, 2023 pm 06:28 PM

How to implement the statement to unlock the table in MySQL? In MySQL, table locks are a commonly used locking mechanism used to protect data integrity and consistency. When a transaction is reading and writing to a table, other transactions cannot modify the table. This locking mechanism ensures data consistency to a certain extent, but may also cause other transactions to be blocked. Therefore, if a transaction cannot continue for some reason, we need to manually unlock the table so that other transactions can continue. MySQL provides a variety of

How to implement the statement of inserting data in MySQL? How to implement the statement of inserting data in MySQL? Nov 08, 2023 am 11:48 AM

How to implement the statement of inserting data in MySQL? When using a MySQL database, inserting data is a very basic and common operation. By inserting data, new records can be added to database tables to provide support for business operations. This article will introduce how to use the INSERT statement in MySQL to implement data insertion operations, and provide specific code examples. The INSERT statement in MySQL is used to insert new records into the database table. Its basic syntax format is as follows: INSERTINTOt

See all articles