PostgreSQL And – Or
AND returns query rows if both conditions are true.
OR returns query rows if at least one of the conditions is true.
And – Or syntax
SELECT * FROM table_name WHERE condition AND condition;
SELECT * FROM table_name WHERE condition OR condition;
SELECT * FROM table_name WHERE condition AND condition OR condition;
And – Or example
Goods table
| id |
good_type |
name |
description |
price |
insert_date |
| 1 |
A |
Car_1 |
Car 1 description |
100 |
2018-07-21 08:45:57.311809 |
| 2 |
A |
Car_2 |
Car 2 description |
200 |
2018-07-21 08:45:57.311809 |
| 3 |
A |
Car_3 |
Car 3 description |
100 |
2018-07-21 08:45:57.311809 |
| 4 |
B |
Boat_4 |
Boat 4 description |
500 |
2018-07-21 08:45:57.311809 |
| 5 |
B |
Boat_5 |
Boat 5 description |
300 |
2018-07-21 08:45:57.311809 |
| 6 |
C |
Train_1 |
Train 123 description |
800 |
2018-07-21 08:45:57.311809 |
SELECT * FROM goods WHERE good_type = 'A' and price=100;
Result
| id |
good_type |
name |
description |
price |
insert_date |
| 1 |
A |
Car_1 |
Car 1 description |
100 |
2018-07-21 08:45:57.311809 |
| 3 |
A |
Car_3 |
Car 3 description |
100 |
2018-07-21 08:45:57.311809 |
SELECT * FROM goods WHERE good_type = 'C' or price=300;
Result
| id |
good_type |
name |
description |
price |
insert_date |
| 5 |
B |
Boat_5 |
Boat 5 description |
300 |
2018-07-21 08:45:57.311809 |
| 6 |
C |
Train_1 |
Train 123 description |
800 |
2018-07-21 08:45:57.311809 |