MySQL and and or introduction
And and or can combine two or more conditions in a where sub-statement.
When using the OR keyword:
- As long as one of these criteria is met, such a record is queried.
- If any of these query conditions are not met, such records will be excluded.
When using the AND keyword:
- All the conditions are met and the records are queried.
- If any one of these conditions is not met, such records will be excluded.
MySQL and and or instances
The table data you need to use in this example is as follows:
title |
content |
category |
Seo_name |
PHP arrays |
PHP Array Usage Analysis |
1 |
Php |
MySQL DISTINCT |
MySQL distinct instances |
2 |
Mysql |
Java array |
How to use Java array |
3 |
Java |
PHP input |
How PHP input is given a value |
4 |
Php |
(1) An and conditional query operator instance:
Use and to display all data with the title "PHP Array" and Category 1:
SELECT * FROM ar WHERE title=‘php数组‘ AND category=‘1‘
Results:
title |
content |
category |
Seo_name |
PHP arrays |
PHP Array Usage Analysis |
1 |
Php |
(2) or conditional operator instance
Use or to display all data with the title "Java array" or Seo_name "PHP":
SELECT * FROM ar WHERE title=‘java array‘ OR seo_name=‘php‘
Results:
title |
content |
category |
Seo_name |
PHP arrays |
PHP Array Usage Analysis |
1 |
Php |
Java array |
How to use Java array |
3 |
Java |
PHP input |
How PHP input is given a value |
4 |
Php |
(3) combining and and OR operators
We can also combine and and or together (using parentheses to form complex expressions):
SELECT * FROM ar WHERE (title=‘java array‘ OR category=‘4‘)AND seo_name=‘php‘
Results:
title |
content |
category |
Seo_name |
PHP input |
How PHP input is given a value |
4 |
Php |
And and or precedence
You can include any number of and and or operators in where, and when there are no other symbols, such as parentheses, SQL executes the and condition first, and then executes the or statement, such as:
select * from table from id=1 or id=2 and price>=10;/* http://www.manongjc.com/article/1439.html */
This statement defaults to id=2 and the price is greater than or equal to 10, or id=1.
If you add parentheses:
select * from table from (id=1 or id=2) and price>=10;
The statement executes id=1 or id=2, and the price is greater than or equal to 10.
Original address: http://www.manongjc.com/article/1439.html
MySQL condition query and or use instance and priority introduction