Home > Database > Mysql Tutorial > body text

mysql advanced (5) multi-condition query with OR in data table

黄舟
Release: 2017-02-09 15:18:00
Original
1207 people have browsed it

Multi-condition query with OR in MySQL data table

The OR keyword can be combined with multiple conditions for query. When using the OR keyword:

Conditions

1) As long as one of these query conditions is met, such records will be queried.

2) If any of these query conditions are not met, such records will be excluded.

Grammar format

The basic syntax format of the OR keyword is as follows:

Conditional expression 1 OR conditional expression 2 [...OR conditional expression n]

OR can connect two conditional expressions, and multiple OR keywords can be used to connect more conditional expressions.

Example

Example 1

Use the OR keyword to query the records in the employee table whose d_id is 1001, or whose sex is 'male'. The code of the SELECT statement is as follows:

SELECT * FROM employee WHERE d_id=1001 OR sex LIKE '男';
Copy after login

The record with d_id 1001 can be queried. At the same time, the record with sex as 'male' has also been queried.

This shows that when using the OR keyword, as long as it meets any one of multiple conditions, it can be queried.

Example 2

Use the OR keyword to query the records in the employee table. The query condition is that the value of num is in the (1,2,3) set, or the range of age is 24 to 26, or the value of homeaddress contains "Xiamen City". The code of the SELECT statement is as follows:

SELECT * FROM employee WHERE
         num IN(1,2,3) OR age BETWEEN 24 AND 26
         OR homeaddress LIK '%厦门市%';
Copy after login

The results show that as long as any one of these three conditional expressions is met, such records will be queried.

OR can be used together with AND. When the two are used together, AND operates before OR.

Example 3

Use OR and AND keywords to query records in the employee table. The code of the SELECT statement is as follows:

SELECT * FROM employee WHERE num IN(1,3,4) AND age=25 OR sex='女';
Copy after login

The results show that as long as the records matching num IN(1,3,4) AND age=25 or the records matching sex='female' are displayed.

If you change the order of the conditions, the SELECT statement becomes the following situation:

SELECT * FROM employee WHERE sex='女' OR num IN(1,3,4) AND age=25;
Copy after login

The result shows that the execution result of the previous SELECT statement is the same.

This shows that the conditions before and after the AND keyword are combined first, and then combined with the conditions of the OR keyword. In other words, AND is calculated before OR.

Tips

AND and OR keywords can connect conditional expressions. Operators such as "=" and ">" can be used in these conditional expressions, as well as keywords such as IN, BETWEEN AND and LIKE. Moreover, the LIKE keyword can use "%" and "_" when matching strings. Wait for wildcards.

The above is the content of the multi-condition query with OR in the mysql advanced (5) data table. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template