SQL学习笔记(sql如何学)
1. create table as ...
在数据量过大的时候,可以先筛选出一些数据创建一张新的表,在新的表上再做匹配
create table tmp as
select * from A
where A.id > 10;
2. substr(col_name, start, length) 截取某一列的子字符串,从下标为start开始,长度为length,如:substr(A.address, 1, 3)即为截取表格A的address列,字符串的前3个字母。
3. decode(col_name,值1,翻译值1,值n,翻译值n,缺省值),如:
decode(A.city, '北京', 1, 0)表示A表格city列,如果值为北京,显示为1,否则表示为0,如果不写缺省值则默认为空,一般与列重命名搭配使用,直接在语句后加上重命名之后的列名即可,如:decode(A.city, '北京', 1, 0) 是否在北京
4. case when A.city = '北京' then 1 else 0 end
表示当表格A的city列等于北京时,显示为1,否则显示为0,如果不写else直接写end则默认其他情况显示为空,一般与列重命名搭配使用,直接在语句后加上重命名之后的列名即可,如:decode(A.city, '北京', 1, 0) 是否在北京
5. 查找包含关键词的语句
select column_name
from table_name
where column_name like pattern,如:
select * from A where number like '%123%'表示查找表格A里的number列中包含字符串123的列
6. is not null表示提取非空字段,如:
select * from A where name is not null表示提取表格A中name列不为空的数据行