Home>Article>Database> What is the usage of or in mysql select statement?

What is the usage of or in mysql select statement?

WBOY
WBOY Original
2022-03-23 10:52:26 5797browse

The or keyword in the mysql select statement is used to set the conditions of the query; as long as one of the query conditions is met, the record will be queried. If it does not meet any of the query conditions, the record will be excluded. out, the syntax is "SELECT*FROM...WHERE condition 1 OR condition 2".

What is the usage of or in mysql select statement?

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

What is the usage of or in mysql select statement

In mysql, we often encounter such a situation. When writing a conditional statement where, there may be multiple conditions of "or" at the same time. ” or “AND”, but often the effect is not achieved. Through Baidu, I found that in a where statement, the condition “AND” or “OR” appears at the same time. Multiple ORs must be enclosed in parentheses and then ANDed. Perform "AND", or wrap multiple ANDs in parentheses and perform "OR" with OR.

When using the OR keyword:

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

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

When using the and keyword:

  • All conditions need to be met, and such records will be queried.

  • If any of the conditions are not met, such records will be excluded.

eg. select * from table from id=1 or id=2 and price>=10;

This statement is executed by default if id=2 and price is greater than or equal to 10, or if id=1.

If you add parentheses:

select * from table from (id=1 or id=2) and price>=10;

, then this statement executes id=1 or id=2, and the price is greater than or equal to 10.

Mixed use of or and and. First of all, it should be clear that compared to or, the former has a higher priority.

Summary: To use a mixture of and and or, the or condition must be included together with () to form a part. Otherwise, the expected effect will not be achieved.

Recommended learning:mysql video tutorial

The above is the detailed content of What is the usage of or in mysql select statement?. For more information, please follow other related articles on the PHP Chinese website!

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