Home > Database > Mysql Tutorial > body text

How to use AND operator and OR operator in SQL statement

WBOY
Release: 2023-05-28 16:34:38
forward
2856 people have browsed it

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
Copy after login


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"
Copy after login

Result:

LastName    FirstName    Address            City
Carter      Thomas       Changan Street     Beijing
Copy after login


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"
Copy after login

Result:

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


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"
Copy after login

Result:

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

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!

Related labels:
source:yisu.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!