SQL多条件查询

时间:2023-02-10 22:30:23

1,用逻辑运算符AND和 OR来联结多个查询条件

eg:

SQL> select * from customers where dob > to_date(‘1970-01-01’, ‘yyyy-mm-dd’) and first_name like ‘D_’;

CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE


      5 Doreen     Blue       20-5月 -70   

eg:

SQL> select * from customers where dob > to_date(‘1970-01-01’, ‘yyyy-mm-dd’) or first_name like ‘C%’;(每个条件的记录合在一起)

CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE


      2 Cynthia    Green      05-2月 -68     800-555-1212                                
3 Steve White 16-3月 -71 800-555-1213
5 Doreen Blue 20-5月 -70 eg:

SQL> select * from customers where customer_id < 3;

CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE


      1 John       Brown      01-1月 -65     800-555-1211                                
2 Cynthia Green 05-2月 -68 800-555-1212

SQL> select * from customers where not customer_id < 3;

CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE


      3 Steve      White      16-3月 -71     800-555-1213                                
4 Gail Black 800-555-1214
5 Doreen Blue 20-5月 -70

SQL> select * from customers where not customer_id >= 3;

CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE


      1 John       Brown      01-1月 -65     800-555-1211                                
2 Cynthia Green 05-2月 -68 800-555-1212

2,使用AND和 OR可以实现其他操作符的功能
[NOT] IN
[NOT] BETWEEN … AND …

3,使用NOT进行逻辑求反操作

4,运算符优先级

比较运算符 > NOT > AND > OR