单表查询(第一章)
1.1查询表中的所有的行与列
select * from table;
1.2根据条件查询【where】
select * from table where column = 'xxx';
1.3查找为空值列【IS NULL】
select * from table where column is null;-->为空
select * from table where column is not null;-->不为空
1.4将空值转换为实际值
select coalesce(null,'2',null,'3','1') as str from dual; -->2
1.5查找满足多条件的行[or < and < not < between_and < is null 、like 、in]
select * from table where column1 = 'xx' or column2 = 'xxx' and column3 = 'xxxx';
1.6从表中检索部分列
select column1,column2,column3 from table;
1.7为列表取有意义的名称【AS】
select column1 as '第一列',column2 as '第二列',column3 as '第三列' from table;
1.8在where子句中引用取别名的列
select * from (select column1 as '第一列',column2 as '第二列',column3 as '第三列' from table) x where 第一列 = 'xxx';
1.9拼接列【||】
select column1 || '拼接' || column2 as str from table;-->第一列的值 拼接 第二列的值
1.10在select语句中使用条件逻辑[case when_then ....when_then.....esle ....end ]
select ename, sal, case
when sal <= 2000 then '过低'
when sal >= 9000 then '过高'
else '一般'
end as '情况'
from table;
1.11限制返回的函数[rownum]
select * from table where rownum <= 2;-->取前两条记录
select * from (select rownum as sn,table.* from table) where sn = 2;-->取第二条数据
1.12从表中随机返回n条记录[dbms_random.value()对数据随机排序,运行顺序:select > where >order by]
select * from
(select * from table order by dbms_random.value())-->先对数据随机排序
where rownum <= 3; -->取排序后的前三条数据
select * from table where rownum <= 3 order by dbms_random.value();-->先取出前三条数据,再对其随机排序
1.13模糊查询[like]
select * from emp where hiredate like '%2月%';
select * from emp where hiredate like '\\_2月%' escape '\';-->对通配符进行转义(_)
注意事项
①、select replace('abc','a',null) as str from dual;[dual是伪表,利用伪表可以设置或查看序列或调用一些内置函数]
oracle数据库返回值 str bc;(只将a的值替代为空)
mySql数据库返回值 str ;(全部被替代为空)
②、select greatest(1,2,3,null) as str from dual;[返回列表中找最大值]
返回值 str null
③、select least(1,2,3,null) as str from dual;[返回列表中找最小值]
返回值 str null
④、select coalesce(null,'2',null,'3','1') as str from dual; [返回列表中第一个不为空的值(列表可以多个值) coalesce() 函数 ]
返回值 str 2
⑤、select nvl(null,'2') as str from dual;[返回列表中第一个不为空的值(列表中只能放两个值) nvl()函数 mySql数据库无该
函数 ]
返回值 str 2
⑥、truncate table tableName[删除表数据,保留表结构]
a.删除表数据,但保留表结构;b,速度快,但不可回滚;c.无法触发任何操作;d.行标识的序号重置。
内置函数