sql 笔记之一

1、select 指定的字段要么要包括在聚合函数中,要么就要跟在group by的后面作为分组的依据。

 PS:可以用Max()函数括起来必须要查的非数字型的字段。

2、distinct 用于返回唯一不同的值

例:select distinct company from orders。--一个公司名称只能查询到一次

3、12356 和12346

union all         12356 12346 (显示所有的数据)

union            结果集       123456          (并集,包含所有的元素,去除重复的值)

intersect                            1236    (交集)

4、一次插入多条数据

a:

insert into tableA (列一,列二)

select 值一,值二

union   /union all     -----union 是相同值记录只插入一次,不重复插入  union all会重复插入

select 值三,值四

b:

insert into tableA(列一,列二) values(值一,值二),(值3,值4),(值5,值6),(值7,值8),(值9,值10)

c:从另一个表中读取多条数据添加到新表中

insert into table(lie1,lie2)

select  a,b from tableA where a=1-----从一个表中读取数据

insert into table(lie1,lie2)

select  a,b from tableA where a=1

union all

select  a,b from tableB where a=2 ---从多个表中读取数据

5、Case when then else end   

---简单的

Case Sex

      when '1' then ‘男’ 

      when '2' then ‘女’

else ‘其他’

END

---搜索函数

Case when sex=1 then  ‘男’ 

                 sex= '2' then ‘女’

else ‘其他’

END

6、decimal(a,b) 规定的是存储的值不会超过a位数字,小数点后面有b位    0=<b=<a

例:decimal(5,2)存储的值不会超过5位数字,小数点后面有2位数字

7、decimal  float double的区别

 

float:浮点型,含字节数为4,32bit,数值范围为-3.4E38~3.4E38(7个有效位)

 

double:双精度实型,含字节数为8,64bit数值范围-1.7E308~1.7E308(15个有效位)

 

decimal:数字型,128bit,不存在精度损失,常用于银行帐目计算。(28个有效位)

float f = 345.98756f;--结果显示为345.9876,只显示7个有效位,对最后一位数四舍五入。

double d=345.975423578631442d;--结果显示为345.975423578631,只显示15个有效位,对最后一位四舍五入。

--注:float和double的相乘操作,数字溢出不会报错,会有精度的损失。float与double相乘结果类型是double

decimal dd=345.545454879.....--可以支持28位,对最后一位四舍五入。

--:当对decimal类型进行操作时,数值会因溢出而报错。

8、with ties

 select top 8 *   from tablett order by state desc

 

select top 8 with ties *   from tablett order by state desc

 

区别就是用了with ties 最后一行的参数值(在这句里是state相同 ) 如果相同的话 会都显示出来

 

额外的返回行的参数值与TOP n(PERCENT)行中的最后一行的该参数值相同。这个地方该怎么理解呢?其实是如果按照order by 参数排序TOP n(PERCENT)返回了前面n(pencent)个记录,但是n+1…n+k条记录和排序后的第n条记录的参数值(order by 后面的参数)相同,则n+1、…、n+k也返回。n+1、…、n+k就是额外的返回值。

 

posted on 2018-06-10 20:10  heihaha  阅读(146)  评论(0编辑  收藏  举报