随笔分类 - SQLSERVER
表表达式
摘要:l表表达式l-> 派生表(临时)•将查询出的数据(结果集)作为from后的数据源•查询使用括号括起来,并命名(必须)•不能在里面使用order by(除非加top)l-> 公用表表达式(CTE, 临时)•语法l-> 视图(vw, 持久)•语法ll视图概述l回顾数据怎么存储的l视图是一张虚拟表,它表示...
阅读全文
SqlServer表连接
摘要:表连接就是将两张表"合并"成一张表 tbl1: 1. 张三 1 2. 李四 2 3. 王五 null tbl2: 1.男 2.女 一. 交叉连接(cross join)--笛卡尔积(叉积) 将第一张表的每一条记录依次与第二张表的每一条记录进行组合,得到一张新表 交叉连接的结果为: 1. 张三 1 1
阅读全文
CASE函数
摘要:-> 使用类似switch-case与if-else if -> 语法 •case [字段] • when 表达式 then 显示数据 • when 表达式 then 显示数据 • else 显示数据 •end as 别名 -> then 后数据类型要一致 简单的例子: 1 select 2 cas
阅读全文
数据库连接字符串
摘要:C#程序中使用,或者在配置文件中配置。 连接字符串 •-> data source=实例名;initial catalog=数据库名;integrated security=true/user id=…;password=…; •-> server=实例名;database=数据库名;integra
阅读全文
联合多表查询
摘要:l-> union就是将多个结果集合并成一个结果集 l-> 查询语句1 union 查询语句2 l-> union会自动合并重复的数据 l-> union all会保留重复数据 •一般都是使用union all,效率会高点 l-> 联合需要注意类型一致 •cast()函数可以实现数据的类型转换 l
阅读全文
数据的排序,分组
摘要:-> 排序order by -> 如果有where筛选,order总放在后面 •select * from … where … order by … -> 多列排序(从左至右) •select * from 表名 order by 字段1 [desc], 字段2 [desc], 字段3[desc]
阅读全文
数据库中空值处理
摘要:查询所有生日为null的姓名 -> select Fname from T_Student Fbirthday is null 判断null只能使用is或is not •is null和is not null -> 函数 isnull(字段名, 替换值) •查询时专门为空值的字段显示处理 •sele
阅读全文
模糊查询
摘要:-> 查询所有姓张的 •select * from Person where Name like ‘张%’ -> 查询姓张并且名字是一个字的 •select * from Person where Name like ‘张_’ -> 查询名字中带亮字的 •select * from Person w
阅读全文
Top
摘要:-> 获取前几条数据,放在列前面,常与order by一起使用 -> 排序 •-> select * from 表名 order by 字段 asc •-> asc表示升序、desc表示降序 -> 查询(百分比向右对齐) •-> select top 3 * from 表名 order by Age
阅读全文
SQLServer 的Distinct
摘要:distinct去除重复的数据(distinct是对整个结果集进行数据重复处理,不是针对某一列) -> 检查返回不重复的数据(对于整条记录不重复才会去除,如ID不一样) 用法:select distinct * from 表名 -> 针对某一列去除重复,检索数据 •select distinct(需
阅读全文
SqlServer增加,删除,修改表结构操作语句
摘要:-> 删除一列 alter table 表名 drop column 列名 -> 增加一列 alter table 表名 add 列名 类型 -> 修改数据类型 alter table 表名 alter column 列名 类型 --手动删除一列(删除EmpAddress列) alter table
阅读全文