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

How to use Lottie animation in React Native project

Lottie is an open source animation library for iO...

Explanation of the problem of selecting MySQL storage time type

The datetime type is usually used to store time i...

Details on overriding prototype methods in JavaScript instance objects

Table of contents In JavaScript , we can usually ...

Tutorial on upgrading, installing and configuring supervisor on centos6.5

Supervisor Introduction Supervisor is a client/se...

Windows Service 2016 Datacenter\Stand\Embedded Activation Method (2021)

Run cmd with administrator privileges slmgr /ipk ...

MySQL primary key naming strategy related

Recently, when I was sorting out the details of d...

How to expand the disk size of a virtual machine

After Vmvare sets the disk size of the virtual ma...

Implementation of CSS Fantastic Border Animation Effect

Today I was browsing the blog site - shoptalkshow...

javascript:void(0) meaning and usage examples

Introduction to void keyword First of all, the vo...

Zabbix's psk encryption combined with zabbix_get value

Since Zabbix version 3.0, it has supported encryp...

MySQL graphical management tool Navicat installation steps

Table of contents Preface 1. Arrange the installa...

How to set the text in the select drop-down menu to scroll left and right

I want to use the marquee tag to set the font scro...

XHTML tags should be used properly

<br />In previous tutorials of 123WORDPRESS....