数据库学习笔记(2)-----where条件语句的用法
在Oracle数据库中,where代表筛选条件,比如,有这么一张表格'sale':
year | month | num | money |
---|---|---|---|
2019 | 1 | 20 | 40 |
2019 | 2 | 15 | 30 |
2018 | 1 | 10 | 20 |
2018 | 2 | 20 | 40 |
如果我们想要查询2019年的数据信息,则:
select * from sale where year = ‘2019’;
最后出来的表格为:
year | month | num | money |
---|---|---|---|
2019 | 1 | 20 | 40 |
2019 | 2 | 15 | 30 |
如果我们想要查询2月份的数据信息,则:
select * from sale where month = ‘2’;
最后出来的表格为:
year | month | num | money |
---|---|---|---|
2019 | 2 | 15 | 30 |
2018 | 2 | 20 | 40 |
如果我们想查询2019年1月的数据,则:
select * from sale where year = ‘2019’ and month = ‘1’;
最后出来的表格为:
year | month | num | money |
---|---|---|---|
2019 | 1 | 20 | 40 |
另:
在涉及到SQL优化的情况下,遵循如下原则:
1、索引的建立、联合主键的建立以where后面跟的条件为主,例如:year、month;
2、where后面的条件语句尽可能避免运算,比如year = 2018+1,因为进行运算之后,一 定会导致全表扫描。