select * from student;
--别名(为字段取一个临时的名称)
select s_name as "姓名",s_age as "年龄",c_id as "班级"
from student;
--两表查询,消除笛卡尔积,并为字段取别名
select s.s_name as "姓名",s.s_age as "年龄",
c.c_name as "班级" from student s,sclass c
where s.c_id=c.c_id;
--连接符号,用于数据库查询时将查询的字段连接在一起
select s.s_name||','||s.s_age||','||c.c_name from
student s,sclass c where s.c_id=c.c_id;
--排序order by(将需要用顺序排列的字段放到order by后面)
select * from student order by s_age desc;
select * from student order by s_age asc;--(默认情况下升序)
--count函数(用于统计和计数)
select count(s_id) from student;
--initcap(字符串格式化,首字母大写,仅限英文)
select initcap('hEllo world') from dual;
--length用于统计字符串长度
select length('') from dual;
--用于指定替换特定的字符
select replace('updown','up','down') from dual;
--substr截取字符串,从指定位置开始截取,指定截取几位字符
select substr('Message',2,4) from dual;
--trim消除字符中第一个和最后一个空格
select trim(' world ') from dual;
--lower将字符的所有的字母小写
select lower('HEllo World') from dual;
--avg求平均数
select avg(s_age) from student;
--创建临时表,测试date数据类型
create table tempA
(
--表字段数据类型使用date日期格式
createDate date
);
select * from tempA;
--插入数据使用
--to_date(“第一参数是日期和时间”,“第二参数是日期和时间的格式化”)表示
--年月日使用yyyy-MM-dd表示
--时分秒:时hh加上24或者12,分用Mi表示。秒用ss表示
insert into tempA values(to_date('2017-03-05 23:12:55','yyyy-mm-dd hh24:Mi:ss'));
--将指定日期格式化输出
select to_date('2017-03-05 23:55:12','yyyy-MM-dd hh24:Mi:ss') from dual;
--查询当前系统日期,并格式化输出使用to_char
select to_char(sysdate,'yyyy-MM-dd hh24:Mi:ss') as nowDate from dual;
--round保留参数的整数部分
select round(123456.01) from dual;
--两个参数,指定保留小数位数
select round(123456.12345,2) from dual;
--第二个参数-1代表将参数1四舍五入
select round(123656,-3) from dual;