SQL语句
2015-11-23 10:27
267 查看
子查询 加一列作为统计
select UserName,Month,Salary,
Cummulation=(
select SUM(Salary)
from
User_Salary i
where
i.UserName=o.UserName and i.Month<=o.Month
)
from User_Salary o
order by 1,2
select
A.*,B.*
from
User_Salary A inner join User_Salary B
ON
A.UserName = B.UserName
where
B.Month <= A.Month
order by 1,2
select
A.UserName,A.Month,MAX(A.Salary) as Salary,SUM (B.Salary) as Accumulation
from
User_Salary A inner join User_Salary B
ON
A.UserName = B.UserName
where
B.Month <= A.Month
group by
A.UserName,A.Month
order by
A.UserName,A.Month
查询特殊符号ESCAPE定义转义符
在使用LIKE关键字进行模糊查询时,“%”、“_”和“[]”单独出现时,会被认为是通配符。为了在字符数据类型的列中查询是否存在百分号(%)、下划线(_)或者方括号([])字符,就需要有一种方法告诉DBMS,将LIKE判式中的这些字符看作是实际值,而不是通配符。关键字ESCAPE允许确定一个转义字符,告诉DBMS紧跟在转义字符之后的字符看作是实际值。
//查询10%
select * from deptre where deptname LIKE '%10!%%' ESCAPE '!'
//查询_
select * from deptre where deptname LIKE '%!_%' ESCAPE '!'
select * from deptre where deptname LIKE '%!_!%%' ESCAPE '!'
select UserName,Month,Salary,
Cummulation=(
select SUM(Salary)
from
User_Salary i
where
i.UserName=o.UserName and i.Month<=o.Month
)
from User_Salary o
order by 1,2
select
A.*,B.*
from
User_Salary A inner join User_Salary B
ON
A.UserName = B.UserName
where
B.Month <= A.Month
order by 1,2
select
A.UserName,A.Month,MAX(A.Salary) as Salary,SUM (B.Salary) as Accumulation
from
User_Salary A inner join User_Salary B
ON
A.UserName = B.UserName
where
B.Month <= A.Month
group by
A.UserName,A.Month
order by
A.UserName,A.Month
查询特殊符号ESCAPE定义转义符
在使用LIKE关键字进行模糊查询时,“%”、“_”和“[]”单独出现时,会被认为是通配符。为了在字符数据类型的列中查询是否存在百分号(%)、下划线(_)或者方括号([])字符,就需要有一种方法告诉DBMS,将LIKE判式中的这些字符看作是实际值,而不是通配符。关键字ESCAPE允许确定一个转义字符,告诉DBMS紧跟在转义字符之后的字符看作是实际值。
//查询10%
select * from deptre where deptname LIKE '%10!%%' ESCAPE '!'
//查询_
select * from deptre where deptname LIKE '%!_%' ESCAPE '!'
select * from deptre where deptname LIKE '%!_!%%' ESCAPE '!'
相关文章推荐
- MySQL数据库读写分离的简单方法
- Hive之insert into 和insert overwrite
- PL/SQL正确选择游标类型
- 简洁的MysqlHelper
- Mysql分库分表方案
- Oracle--用户管理与权限分配
- ORACLE用JDBC操作CLOB
- oracle分配权限 学习笔记--转载
- PL/SQL开发中动态SQL的使用方法
- 基于Oracle的高性能动态SQL程序开发
- SQL 回炉系列(一)
- 动态SQL和PL/SQL的EXECUTE选项分析
- spring + redis 实现数据的缓存
- Memcached在使用中常用到的方法
- 修改服务器数据库密码
- mysql 查询父级
- mysql 查询父级
- redis
- MYSQL 编程之重连篇
- C mysql