sql server 笔记

来源:互联网 发布:阿里云pc 编辑:程序博客网 时间:2024/06/07 17:36
最基本的SQL查询语句:
 select (table fields list)表的字段列表
 from (table names list)表名称列表
 where (row constraints specification)行约束规范
 group by (grouping specification)分组规范
 having (grouping selection specification)分组选择规范
 order by (order rules specification) 秩序规则的规范


select *(star 星号:表的所有列 )实际工作中尽量少用 * (因为所有数据量太大)


select (所需要查询的类型如:ID,name,ProductNumber,Color,Size 等等) from (所要查询的表)




select (要查询的类:Listprice)from (要查询的表) order by (指定某一类项:Listprice ) desc desc ==descending order;倒序排列 asc ==ascending order ;正序排列 


 

select (要查询的类:name,listprice) from (要查询的表) order by (指定类 listprice) desc (指定类 name)



select (要查询的类 producyId,name) from (要查询的表) order by (2 数字表示查询类的第二项) 正序排列




select (要查询的类 name , isnull(Color,""):表示Color里面包含 null的显示为 “”空 ) from (要查询的表 )




select (要查询的类(表头名) isnull(Color,"") as Color, ::表示isnull设置color显示为空 现用 as 关键字给为空的color 起别名 为color ) isnull(Size,"") as 别名, from (要查询的表)


select (要查询的类 ‘字符串’+ 类名(表头标题名)) from (要查询的表) ::表示字符串和表内容 连接




var rate = 每小时工资 ; 价格/小时*40小时/周*52周/年= 年薪; 别名就是年薪 select (ID,rate*40*52 as 别名 ,round(rate*40*52,1) as 别名 ,round(rate*40*52,0) as 别名 ) from (要查询的表) 1 表示小数点后一位 : 12.10 0 表示小数点后0 进位:12.00




select ( ID, (rate+5)*40*52 as 别名 ) from (要查询的表 ) (rate+5)*40*52 :四则运算法 括号里面优先计算




select (查询类) from (要查询的表) where ID(int:整数型)使用 < > = >= <= select (查询类) from (要查询的表) where Number="so43589" (string:字符型) :特值某个字符“” where 语句中可使用 and谁和谁 or谁或者谁




wildcard:通配符 select * from (查询表) where 类别 like '%123%' %123% :所有只要包含123的字符 %123 :所有结尾含有123的字符 123% :所有开头含有123的字符


select * from 查询表 where 类 in ("类所包含的项","类所包含的项","类所包含的项") select * from 查询表 where 类 not in('类所不包含的项','类所不包含的项') select * from 查询表 where 类 is null ::表示查询这个类所有为 null 的 select * from 查询表 where 类 is not null ::表示查询这个类所有不为 null 的 select * from 查询表 where 类:类的所指定项 or 类:类的所指定项 或 select * from 查询表 where 类:类的所指定项 and 类:类的所指定项 和 扩展 select * from 查询表 where (谁 or 谁) and (谁 and s谁)嵌套使用 




select count(查询类) from (查询表) where is not null ::count 查询这样的数据一共有多少条 select distinct(查询类) from (查询表) where is not null ::distinct 查询这样独一无二的数据 select count(distinct(查询类)) from (查询表) where is not null ::count+distinct 查询这样独一无二的数据一共有多少条


select Avg(类) as 别名:平均数 ,Min(类) as 别名:最小数 ,Max(类) as 别名:最大数 ,Sum(类) as 别名:总和 from (查询表)




select (类) ,Max(类)as 别名 from (查询表) where (类) is not null group by (类) **Max前必须把前面所有的类放在这里 order by (类) 顺序排列




select 类(title),Max(类)as 别名 from (查询表) where 类 is not null (筛选不为空的) group by 在Max前面的所有类 having Max(类)>15000 ::特指 Max的条件项 order by 类 ::顺序排列




select 类(title),Max(类) as 别名 ::上面使用Max下面就必须使用group by Max前面的类 from (查询表) where 类 is not null and 类 >= '2017/1/1' ::筛选不为空的类和大于等于2017的项 group br 类 ::添加所有在Max前面的类项 having Max(类) > 14000 ::特定Max的条件筛选项 order by 类 ::顺序排列 desc :倒序排列