SQL课堂笔记

--注释
公司里一般而是用绝不重复的guid()做主键(web项目不常用)
如null参与运算,结果都是null
在数据库中创建索引能提高查询效率,)只在经常要检索的字段创建索引)
 
sql查询null的数据
selsct * from table 
where name is null
 
查询年龄介于20到30的数据
selsct * from table 
where age between 20 and 30
 
查询年龄是20,22,32,46的数据
selsct * from table 
where age in(20,22,32,46)
 
查询年龄大于20的数据。按升序排序  DESC:降序  ASC:升序
selsct top 3 * from table 
where age>20        //先条件后排序,order by必须在where语句之后
order by age ASC
 
根据年龄进行分组,并计算出每个年龄的人数
select age ,count(*) from table
group by age
 
 
查询出来的数据根据年龄进行分组,并计算出每个年龄的人数,并计算出同年龄的平均工资,只显示同年龄人数大于1的
select age ,avg (工资),count(*) from table
group by age
having count(*)>1    //having对分组后的信息过滤,只能用select中的列
 
子查询
select top 3 * from table
where 主键 not in (select top 5 工资 from table order by 工资 desc)
order by 工资 desc
 
修改表结构 增加字段
alter table 表名 add 列名 数据类型
 
去重复查询
select tistinct 工资,姓名 from table 针对显示的整行一样则不重复
 
 
union 将两个查询结果合并到一起,上下两个数据类型要相容,默认去掉重复数据
select 姓名,年龄 from table1
union )这里加all则不去重复,显示全部)
select 姓名,年龄 from table2
)可多行合并)
 
sql 函数
 
ABS():求绝对值
CEILING))舍入到最大整数
floor))舍入到最小整数
round)要修改的数,取小数点后几位)四舍五入
len)计算字符串长度)
lower()upper()装换大小写
ltrim))字符串左空格去掉
rtrim))字符串右空格去掉,去两边则嵌套两函数
substring()(字符串,从哪截取,截取长度是)
 
转换类型函数
cast(‘123‘ as int)
convert(datetime.‘2012-01-01‘)
select isnull(字段,‘名字‘) from table   --将字段中的null换成指定名字
 
日期函数
getdate))取当前日期
dateadd)单位,数量,待计算日期) 
dateadd)month,-8,date)为计算日期date的8个月之前的日期
datediff(单位,开始日期,结束日期)计算两个日期之间的差额
datepart)单位,日期)返回日期的特定部分
统计每年入职的员工人数
select datepart)year,入职时间) count)*)from 表
group by datepart)year,入职时间)
 
case 函数用法:
select 名字
)
case 编号
when 1 then ‘用户‘
when 2 then ‘会员‘
else ‘未知’
end
) as 客户类型
from 表名
 
 
select o.no,c.name,c.age
form Table1 as o join Table2 as c on o.id=c.id
where c.age>=20;
 
子查询
 
--单列子查询
select * from table
where age>(select min(age) from table)
 
 
 
sql分页
select * from
(
select row_number() over(order by Fsalary desc) as rownum,
* form table
) as e1
where e1.rownum>=3 and e1.rownum<=5
 
 
 
【SQL 经典习题】
 
 
表中有ABC三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,
当B列大于C列是选择B列否则选择C列。
select)case when a>b a else b end),
(case when b>c b else c end)
from table
 
在列“客户类型”中显示,当编号等于1的时候显示‘用户’,
当编号等于2的时候显示‘会员’,其他编号显示‘未知’
select name
)
case 
when 工资<2000 then ‘低收入‘
when 工资>2000 and 工资<5000 then ‘中等收入‘
else ‘高收入’
end
)
from table
 
 
--查询通话时长前五条记录
select top 5 * from table
order by DateDiff(second,StartDateTime,EndDateTime) desc
 
--输出所有数据中拨打长途号码)0开头)的总时长
select sum(datediff(second,StartDateTime,EndDateTime)) from table
where tellnum like ‘0%‘
 
--输出本月通话总时长最多的前三名呼叫员编号
select top 3 sum(second,StartDateTime,EndDateTime)callernumber from table
where datediff(month,StartDateTime,getdate  )=0;
group by CallerNumber
order by sum(datediff(second,StartDateTime,EndDateTime)) desc
 
--输出本月拨打电话次数最多的前三个呼叫员的编号
select top 3 CallerNumber from table
where datediff(month,StartDateTime,getdate  )=0;
group by CallerNumber
order by Count(*) desc
 
--输出所有数据的拨号流水)呼叫员编号,对方号码,通话时长),并且在最后一行添加总呼叫时长,汇总【市总】【长总】
 
select CallerNumber,tellnum,DateDiff(second,StartDateTime,EndDateTime)
form table
union all
select N‘汇总‘,
Convert(varchar(50),sum((
case 
when tellnum like ‘0%‘ then DateDiff(second,StartDateTime,EndDateTime)
else 0
end
))),
sum((
case 
when tellnum not like ‘0%‘ then DateDiff(second,StartDateTime,EndDateTime)
else 0
end
))
from table

SQL课堂笔记,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。