最近用SQL Server,重温了下数据库操作, 有很多搜索了才知道用法,于是写点笔记,没事看看。
1、 升序 ASC 降序 DESC
2、二级排序 order by 字段1 ASC , 字段2 DESC
3、多列合并去重 select distinct 字段1, 字段2 from 表名
注:如果指定了select distinct 那么order by 子句中的项就必须出现在选择列表中
4、DateTime比较 where DATEDIFF(day, GETDATE(), 时间字段) > 0
注:DATEDIFF结果是后者减去前者。
5、 where 字段 is null
6、查询某字段值重复的所有记录
select * from 表名 where 字段名 in (
select 字段名 from 表名 group by 字段名 having count(字段名) >1 )
7、分页
select * from (select ROW_NUMBER() over (order by ypmc asc) as rn, ypmc, cdmc, gg, dw, bz, lsj
from HYyycg
where '1' = '1' + sqlwhere
) temp
where rn >= (page-1)*pagesize+1 and rn <=page*pagesize