Mysql multi-condition query statement with And keyword

Mysql multi-condition query statement with And keyword

MySQL multi-condition query with AND keyword. In MySQL, you can use the AND keyword to connect two or more query conditions. Only records that meet all the conditions will be returned.

SELECT * | {field name 1, field name 2, ...}
FROM table name WHERE conditional expression 1 AND conditional expression 2 […… AND conditional expression n];

Query the student table, where the id field value is less than 16 and the gender field value is nv.

It can be seen that the query conditions must be met before they are returned.

Query the student table for records where the id field value is between 12, 13, 14, and 15, the name field value ends with the string "ng", and the grade field value is less than 80.

It can be seen that the returned records satisfy the three conditional expressions connected by the AND keyword at the same time.

PS: Let's take a look at MySQL multi-keyword multi-field fuzzy query

Suppose there are two pieces of data:

(table name is user)

1) username=admin, password=000000

2) username=admin, password=123456

The effect we want to achieve is to be able to enter multiple keyword queries, with multiple keywords separated by commas.

Take the above table as an example: Entering the single keyword "admin" can find these two pieces of data, and entering "admin,000000" only finds the first piece of data. The sql statement that can be implemented is:

select * from user where concat(username, password) like '%admin%';
select * from user where concat(username, password) like '%admin%' and concat(username, password) like '%000000%';

The function of concat is to concatenate strings, but there is a problem: if you enter a single keyword "admin000000", the first data will also be found, which is obviously not the result we want. The solution is: because multiple keywords are separated by commas, it means that commas will never become part of keywords, so we can solve this problem by separating each field with commas when concatenating strings. The following SQL statement will not query the first data:

select * from user where concat(username, ',', password) like '%admin000000%';

If the separator is a space or other symbol, change ',' to 'separator'.

Summarize:

select * from table name where concat(field1, 'separator', field2, 'separator', ...fieldn) like '%keyword1%' and concat(field1, 'separator', field2, 'separator', ...fieldn) like '%keyword2%' ......;

The above is the Mysql multi-condition query statement with And keyword introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • MySQL learning prerequisites for querying data
  • Essential conditional query statements for MySQL database
  • MySQL conditional query and or usage and priority example analysis
  • MySql multi-condition query statement with OR keyword
  • How to use case when statement in MySQL to implement multi-condition query
  • A comprehensive summary of common operations of MySQL conditional query statements

<<:  Tutorial on using iostat command in Linux

>>:  Detailed process of creating a VR panoramic project using React and Threejs

Recommend

Horizontal header menu implemented with CSS3

Result:Implementation Code html <nav class=&qu...

Detailed explanation of Vue's ref attribute

Summarize This article ends here. I hope it can b...

Summary of commonly used multi-table modification statements in Mysql and Oracle

I saw this question in the SQL training question ...

Detailed analysis and usage of tcpdump command under Linux

Introduction To put it simply, tcpdump is a packe...

How to quickly insert 10 million records into MySQL

I heard that there is an interview question: How ...

Detailed explanation of Apache website service configuration based on Linux

As an open source software, Apache is one of the ...

Vue globally introduces scss (mixin)

Table of contents 1. mixin.scss 2. Single file us...

Is a design that complies with design specifications a good design?

In the past few years of my career, I have writte...

Vuex implements simple shopping cart function

This article example shares the specific code of ...