• notice
  • Congratulations on the launch of the Sought Tech site

Multi-condition query statement with mySQL with and keyword

MySQL multi-condition query with AND keyword.In MySQL, using AND keyword can connect two or more query conditions.Only records that meet all 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 name of the student whose id field value is less than 16, and the gender field value is nv in the student table

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

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

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

PS: Let’s take a look at mysql and more 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 enter multiple keyword queries, separated by commas.

Use the above table as an example: Enter a single keyword "admin" to find out the two pieces of data, and enter "admin,000000" to find out only the first piece of data.The achievable sql statement 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 role of concat is to connect strings, but this has a problem: if you enter a single keyword "admin000000", the first piece of data will also be found, which is obviously not the result we want.The solution is: due to the use of Separating multiple keywords by commas means that commas will never be part of the keywords, so we can solve this problem by separating each field with a comma when connecting the string.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 symbols, just modify the',' to be the'separator'.

Summary:

select * from table name where concat(field 1,'separator', field 2,'separator',...field n) like'% keyword 1%' and concat(field1,'separator', field2,'separator',...field n) like'%keyword 2%'......;

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

Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

buy lipitor 80mg pill & lt;a href="https://lipiws.top/"& gt;lipitor price& lt;/a& gt; buy lipitor 10mg generic

Zbzrqr

2024-03-09

Leave a Reply

+