Summary of MySQL database like statement wildcard fuzzy query

Summary of MySQL database like statement wildcard fuzzy query

MySQL error: Parameter index out of range (1 > number of parameters, which is 0)——Summary of MySQL database like statement wildcard fuzzy query

Introduction Today, when using MySQL statements to perform add, delete, modify and query operations, the console reported the following error: Parameter index out of range (1 > number of parameters, which is 0). Translated, it means: the result data is 1, the true value should be 0, the parameter is out of bounds, and an error occurs. This also makes it clear where we made a mistake - the parameter, that is, the handling of the wildcard "?". This article analyzes the errors encountered when executing common SQL statements and using the like statement for wildcard fuzzy queries, and provides solutions.

insert image description here


1. Analyze SQL statements

1. Query analysis of common SQL statements

First, let's analyze the SQL statement I used before:

String sql = "select count(*) from tab_route where cid = ?";

Note : We directly use the wildcard "?" to replace the parameter and use the Spring framework's JdbcTemplate to perform add, delete, modify and query operations. There is no problem here.

2. How to process common SQL query statements

If an error occurs here, please resolve it as follows:

Check the format of the wildcard "?" . Note that it is entered in the English input method, not the Chinese question mark. Check the SQL statement. The parameters passed using the wildcard are not quoted. For example, the following is wrong:

String sql = "select count(*) from tab_route where cid = '?'";

3. Use the like wildcard fuzzy query statement analysis

Similarly, we still use the above method to perform fuzzy query operations and take a look at the SQL statement that fails:

String sql = "select count(*) from tab_route where rname like '%?%';

The execution result is an error : Parameter index out of range (1 > number of parameters, which is 0) .

Result analysis : The result data is 1, the true value should be 0, the parameter is out of bounds, and an error occurs.

We make it clear that there is no problem with the following statement:

String sql = select * from tab_route where rname like '%张%';

This also makes it clear where we made a mistake - the parameter, that is, the handling of the wildcard "?" in the like statement .

2. Analysis of fuzzy query using wildcards in like statements

1. Application scenarios of like statements

Using the like wildcard to perform fuzzy queries is something we often encounter in projects, such as fuzzy queries for data in the search box.

2. Fuzzy query analysis

It is not possible to parse '%?%' directly in the SQL statement, so we need to process the connected string, use the dynamic concatenation concat() method to connect the contents of '%?%' , and then perform the add, delete, modify and query operations.

concat(str1, str2, str3...) will generate a new string

3. Correct sentences

String sql = "select count(*) from tab_route where rname like concat('%',?,'%')";

3. MyBatis like fuzzy query and keyword distinction

When using like fuzzy query in MyBatis, please note that the keyword is: #{str} and the query clause is:

select * from table where name like concat('%',#{name},'%');

If you add a sort field, please note that the keyword is: ${str} and the query clause is:

select * from table where name like concat('%',#{name},'%') order by ${id};

Summary This article analyzes the errors encountered when executing ordinary SQL statements and using like statements for wildcard fuzzy queries, and gives corresponding solutions. At the same time, the operation of fuzzy query in MyBatis and the distinction between different keywords are supplemented. The road to coding is long, so I hope you will be careful.

insert image description here

This is the end of this article about the summary of MySQL database like statement wildcard fuzzy query. For more relevant MySQL fuzzy query like content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL fuzzy query usage (regular, wildcard, built-in function)
  • Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)
  • A brief discussion on the implementation of fuzzy query using wildcards in MySQL
  • A brief discussion on wildcard escape in MySQL fuzzy query
  • MYSQL Must-know Reading Notes Chapter 8: Using Wildcards for Filtering
  • Things to note when using wildcards in MySQL
  • Detailed introduction to the use of MySql like fuzzy query wildcards
  • mysql regular expression LIKE wildcard
  • mysql wildcard (sql advanced filtering)

<<:  Analysis of 2 Token Reasons and Sample Code in Web Project Development

>>:  Some suggestions for improving Nginx performance

Recommend

Detailed explanation of mixed inheritance in Vue

Table of contents The effect of mixed inheritance...

Is it necessary to create a separate index for the MySQL partition field column?

Preface Everyone knows that the partition field m...

Introduction to Jenkins and how to deploy Jenkins with Docker

1. Related concepts 1.1 Jenkins Concepts: Jenkins...

Detailed explanation of the background-position percentage principle

When I was helping someone adjust the code today,...

Complete steps to achieve high availability with nginx combined with keepalived

Preface In order to meet the high availability of...

CentOS7 64-bit installation mysql graphic tutorial

Prerequisites for installing MySQL: Install CentO...

Avoiding Problems Caused by Closures in JavaScript

About let to avoid problems caused by closure Use...

Implementation of MySQL scheduled database backup (full database backup)

Table of contents 1. MySQL data backup 1.1, mysql...

Implementing WeChat tap animation effect based on CSS3 animation attribute

Seeing the recent popular WeChat tap function, I ...

How to quickly import data into MySQL

Preface: In daily study and work, we often encoun...