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

Detailed tutorial for installing ElasticSearch:7.8.0 cluster with docker

ElasticSearch cluster supports動態請求的方式and靜態配置文件to ...

MySQL million-level data paging query optimization solution

When there are tens of thousands of records in th...

960 Grid System Basic Principles and Usage

Of course, there are many people who hold the oppo...

Detailed process of installing the docker plugin in IntelliJ IDEA (2018 version)

Table of contents 1. Development Environment 2. I...

MySql Sql optimization tips sharing

One day I found that the execution speed of a SQL...

Detailed tutorial on installing Docker and docker-compose suite on Windows

Table of contents Introduction Download and insta...

MySQL 8.0.22.0 download, installation and configuration method graphic tutorial

MySQL 8.0.22 download, installation and configura...

VUE implements timeline playback component

This article example shares the specific code of ...

Getting Started with Vue 3.0 Custom Directives

Table of contents 1. Custom instructions 1. Regis...

How to use Linux locate command

01. Command Overview The locate command is actual...

Native js to implement 2048 game

2048 mini game, for your reference, the specific ...

How to install and configure mysql 5.7.19 under centos6.5

The detailed steps for installing mysql5.7.19 on ...

JavaScript - Using slots in Vue: slot

Table of contents Using slots in Vue: slot Scoped...