SQL知识整理
一:Select语句:
select 字段名 from 表名 where 条件 order by 排序
see:select distinct 从多个相同字段中抓唯一值
see:查找ld_det_andy表中有数据但ld_det_temp表中没数据的数据
select * from ld_det_andy a where
(not exists (select * from ld_det_temp where ld_loc = a.ld_loc and ld_part = a.ld_part))
see:选出累计访问量最大的10个ip地址,并按访问量降序排列。
select top 10 ip, countip from
(select ip,count(*) as countip from records group by ip) a
order by countip desc
see:表student(id,name,score)根据分数列(score)每10分为一段,查询每段分数的人数
select ScoreRank,count(*) from
(select ScoreRank = case
when score >= 0 and score < 10 then '0-9'
when score >= 10 and score < 20 then '10-19'
when score >= 20 and score < 30 then '20-29'
when score >= 30 and score < 40 then '30-39'
when score >= 40 and score < 50 then '40-49'
when score >= 50 and score < 60 then '50-59'
when score >= 60 and score < 70 then '60-69'
when score >= 70 and score < 80 then '70-79'
when score >= 80 and score < 90 then '80-89'
&nb
相关文档:
Cross Apply使表可以和表值函数结果进行join, 这样表值函数的参数就可以使用一个结果集,而不是一个标量值,下面是book online的原文,有例子,有解释。
The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function act ......
SQL Server 系统全局变量
@@CONNECTIONS
返回自上次启动以来连接或试图连接的次数。
@@CURSOR_ROWS
返回连接上最后打开的游标中当前存在的合格行的数量(返回被打开的游标中还未被读取的有效数据行的行数)
@@DATEFIRST
返回每周第一天的数字
@@ERROR
返回最后执行的SQL 语句的错误代码。
@@FETCH_STATUS
返 ......
-- FUN:存储过程分页
-- @Table nvarchar(255), -- 表名
-- @Fields nvarchar(1000) = ' * ', -- 需要返回的列
-- @OrderField nvarchar(255), -- 排序的字段名,一般为唯一标识
-- @OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
-- @PageSize int = 10, -- 每页有多少条记录
-- @PageIndex int = 1, -- 第 ......
SQL宝典
SQL必知必会第三版
SQL入门经典第四版
Sams Teach Yourself SQL in 10 Minutes Third Edition
SQL The Complete Reference
......
应一个朋友的要求,贴上收藏的SQL常用分页的办法~~
表中主键必须为标识列,[ID] int IDENTITY (1,1)
1.分页方案一:(利用Not In和SELECT TOP分页)
语句形式:
SELECT TOP 页记录数量 *
from 表名
WHERE (ID NOT IN
(SELECT TOP (每页行数*(页数-1)) ID
from 表名
ORDER BY ......