Mysql multi-condition query statement with And keyword, mysql keyword

Source: Internet
Author: User

Mysql multi-condition query statement with And keyword, mysql keyword

MySQL multi-condition query with AND keyword. in MySQL, The AND keyword can be used 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 condition expression 1 AND condition expression 2 [...... AND condition expression n];

In the student table, the student name whose id field value is less than 16 and whose gender field value is nv

It can be seen that the query conditions must all be met before returning

In the student table, the id field value is in the range of 12, 13, 14, and 15. The name field value ends with the string "ng" and the grade field value is less than 80.

We can see that the returned record meets the three conditional expressions connected by the AND keyword.

PS: Let's take a look at the mysql multi-Keyword multi-field fuzzy query.

Assume there are two data types:

(Table Name: user)

1) username = admin, password = 000000

2) username = admin, password = 123456

The effect we want to achieve is that you can enter multiple keywords for query. Multiple keywords are separated by commas.

Use the preceding table as an example: Enter the keyword "admin" to check the two data items. Enter "admin, 000000" to check only the first data. The SQL statement is as follows:

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

Concat is used to connect strings, but there is a problem: If you enter the single keyword "admin000000", the first data will also be found. This is obviously not the result we want. The solution is: because multiple keywords are separated by commas, the comma will never be part of the keyword. Therefore, when connecting strings, we can separate each field with commas to solve this problem, the following SQL statement does not query the first data:

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

If the Delimiter is a space or other symbols, modify 'and' to 'delimi.

Summary:

Select * from table name where concat (Field 1, 'delimiter', Field 2, 'delimiter ',... field n) like '% keyword 1%' and concat (Field 1, 'delimiter', Field 2, 'delimiter ',... field n) like '% keyword 2% '......;

The above is a multi-condition query statement with the And keyword in Mysql. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.