Select查询语句

Select查询语句

1.1 查询语句
select [distinct] 字段名1 [as 别名1], 字段名2 [as 别名2], …
from 表名
[where 查询的条件];

1.2 按照范围进行查找
between … and … – 表示查询在…和…之间的内容,而且是闭区间
in … – 表示查询在…里面的任何内容
not in … – 表示查询不在…里面的数据内容

1.3 按照条件进行模糊查询
like … – 表示像…,通常进行模糊查询,搭配使用的通配符有:
_ – 表示任意一个字符
% – 表示任意多个字符
not like … (了解)

(例)

-- 查询电话中以“1387”开头的学生信息 student学生表
               select * from student 
               where phone like '1387%';

– 查询学号为S1101004的指定1,2,3科目考试成绩 result成绩表
select studentno, studentresult, subjectid from result
where studentno = ‘S1101004’ and subjectid in (1, 2, 3);

1.4 order by子句
select [distinct] 字段名1 [as 别名1], 字段名2 [as 别名2], …
from 表名
[where 查询的条件];
[order by 字段/别名/位置编号 asc/desc] ; – 默认是从小到大排序(asc)

1.5 字符串相关的常用函数
如:
– 实现计算字符串’hello’的长度
select length(‘hello’) 字符串长度 from dual;
– 实现将字符串’hello’转换为大写
select upper(‘hello’) 转换为大写 from dual;
– 实现将字符串’HELLO’转换为小写
select lower(‘HELLO’) 转换为小写 from dual;
– 实现将字符串’hello’中的首字母转换为大写
select initcap(‘hello’) 首字母转大写 from dual;
– 实现将字符串’hello’中从下标2开始取出3个字符,下标从1开始
select substr(‘hello’, 2, 3) 获取子串 from dual; – ell
– 实现将字符串’hello’和’world’进行拼接
select concat(‘hello’, ‘world’) 拼接字符串 from dual; – helloworld
– 实现字符串中内容的替换,将’e’替换为’E’
select replace(‘hello’, ‘e’, ‘E’) 字符串替换 from dual; – hEllo
– 实现字符串中子串的查找,查找’l’第一次出现的下标位置
select instr(‘hello’, ‘l’) 子串查找 from dual; – 3
– 实现去除字符串中两端的空白字符
select length(trim(’ hello ')) 去除空白字符 from dual;

1.6 数值相关的常用函数
如:
– 查询数据3.1415926保留3位小数并四舍五入后的结果
select round(3.1415926, 3) from dual; – 3.142
– 查询数据保留0位小数,也就是取整数并四舍五入的结果
select round(3.1415926, 0) from dual; – 3
– 查询数据保留-1位小数,也就是将整数的最后一位进行四舍五入
select round(13.1415926, -1) from dual; – 10

– 查询数据3.1415926保留3位小数, 没有四舍五入后的结果
select trunc(3.1415926, 3) from dual; – 3.141
– 查询数据保留0位小数,也就是取整数并没有四舍五入的结果
select trunc(3.1415926, 0) from dual; – 3
– 查询数据保留-1位小数,也就是将整数的最后一位不进行四舍五入
select trunc(13.1415926, -1) from dual; – 10

1.7 日期相关的函数
to_date() - 主要用于将字符串类型转换为日期类型后插入到数据库中;
to_char() - 主要用于将日期类型转换为字符串类型后从数据库中取出数据;
如:
– 查询当前系统时间
select sysdate from dual;
– 查询当前系统时间的明天
select sysdate+1 from dual;

– 查询员工表中员工的编号、名字以及入职日期,要求入职日期按照年月日
select id, first_name, to_char(start_date, ‘yyyy-mm-dd’) 入职日期 from s_emp;
– 向员工表中插入编号为’1005’, ‘张斌’,to_date(‘2019-07-23’, ‘yyyy-mm-dd’)
insert into s_emp (id, first_name, start_date) values(‘1005’, ‘张斌’,to_date(‘2019-07-23’, ‘yyyy-mm-dd’));

– 查询当前系统时间的下一个月
select add_months(sysdate, 1) from dual;

– 查询当前系统时间+1个月+1天+1小时+1分钟后的时间
select to_char(add_months(sysdate, 1) + 1 + 1/24 + 1/24/60, ‘yyyy-mm-dd hh24:mi:ss’) 处理后的时间 from dual;

– 查询当前系统时间并按照dd进行截取,也就是dd后面的数据全部丢弃
select to_char(trunc(sysdate, ‘dd’), ‘yyyy-mm-dd hh24:mi:ss’) from dual;

练习讲解:
– 求当前月份的最后一分钟0秒表示的date数据
– 2019-7-31 23:59:00
select to_char(trunc(add_months(sysdate, 1), ‘mm’)-1/24/60, ‘yyyy-mm-dd hh24:mi:ss’) from dual;

– 查询s_emp表格(id,last_name,start_date),按照start_date排序
– 查询的条件为: 入职日期在2018年1月1日 到2018年12月31日
select id, last_name, start_date from s_emp
where start_date between to_date(‘2018-01-01’, ‘yyyy-mm-dd’)
and to_date(‘2018-12-31’, ‘yyyy-mm-dd’)
order by start_date;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值