Home  >  Article  >  Database  >  mysql advanced (5) multi-condition query with OR in data table

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

黄舟
黄舟Original
2017-02-09 15:18:001202browse

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 '男';

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 '%厦门市%';

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='女';

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;

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)!


Statement:
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