Home>Article>Database> How to use AND operator and OR operator in SQL statement

How to use AND operator and OR operator in SQL statement

WBOY
WBOY forward
2023-05-28 16:34:38 2832browse

SQL AND & OR Operator
The AND and OR operators are used to filter records based on more than one condition.
AND and OR combine two or more conditions in a WHERE substatement.
If both the first and second conditions are true, the AND operator displays a record.
If only one of the first condition and the second condition is true, the OR operator displays a record.
"Persons" table:

LastName FirstName Address City Adams John Oxford Street London Bush George Fifth Avenue New York Carter Thomas Changan Street Beijing Carter William Xuanwumen 10 Beijing


AND operator example
Use AND to display all people with the last name "Carter" and the first name "Thomas" People:

SELECT * FROM Persons WHERE FirstName="Thomas" AND LastName="Carter"

Result:

LastName FirstName Address City Carter Thomas Changan Street Beijing


OR Operator Example
Use OR to display all people whose last name is "Carter " Or someone named "Thomas":

SELECT * FROM Persons WHERE firstname="Thomas" OR lastname="Carter"

Result:

LastName FirstName Address City Carter Thomas Changan Street Beijing Carter William Xuanwumen 10 Beijing


Combining AND and OR operators
We can also combine AND and OR (using parentheses to form complex expressions):

SELECT * FROM Persons WHERE (FirstName="Thomas" OR FirstName="William") AND LastName="Carter"

Result:

LastName FirstName Address City Carter Thomas Changan Street Beijing Carter William Xuanwumen 10 Beijing

The above is the detailed content of How to use AND operator and OR operator in SQL statement. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete