zoukankan      html  css  js  c++  java
  • sql笔记

    熟练一下group by的用法
    order by

    数据为汉字时加N  如where name=N'张三' 
    null 为"不知道" ,而不是空值 5+NUll 也等于 Null, 等于null是查不出来的,
    要查出null的值,用 is null , 不为空就是 is not null

    select Age,count(*) from emloyee group by age  //将每个年龄分组, 并且计算总数,而且是group by什么,就select的字段必须出现在group by 中,(聚合函数除外)

    聚合函数不能出现在where语句中, 所以要用having. 但having不能直接代替where, having只能放这组的过滤信息
        select age,count(*) from employee group by age having count(*)>2 这是正确的
    如: select age,count(*) from employee group by age having salary>2000 //这是错误的,having是对信息分组后的过滤 having中只能放select中字段的过滤信息,having放在group by 的后面


    从第几条到第几条的数据
    select top 3 * from employee
    where Fnumber not in (select top 5 Fnumber from employee order by Fsalary desc)
    order by Fsalary desc

    sqlserver2005 增加了Row_Number函数实现 //读取从第几条到第几条的数据

    模糊查询
    SQL模糊查询,使用like比较关键字,加上SQL里的通配符,请参考以下:
    1、LIKE'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden)。
    2、LIKE'%inger' 将搜索以字母 inger 结尾的所有字符串(如 Ringer、Stringer)。
    3、LIKE'%en%' 将搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。
    4、LIKE'_heryl' 将搜索以字母 heryl 结尾的所有六个字母的名称(如 Cheryl、Sheryl)。
    5、LIKE'[CK]ars[eo]n' 将搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。
    6、LIKE'[M-Z]inger' 将搜索以字符串 inger 结尾、以从 M 到 Z 的任何单个字母开头的所有名称(如 Ringer)。
    7、LIKE'M[^c]%' 将搜索以字母 M 开头,并且第二个字母不是 c 的所有名称(如MacFeather)。


    消除重复数据

    distinct 只会消除正行的重复信息
    如 select distinct Fdepartment,Fsubcompany from T_empoyee,中只会消除 Fdepartment和Fsubcompany 都重复的信息

    联合信息表
    Union 将两个结果集结合在一起,select 的字段个数,类型必须是一致,不过会将重复的数据合并掉, 只显示一条
    union all  可以显示重复行  // 一般用 union all

    常用的方法报表
    显示临时工的和正式工的最高年龄和最低年龄
    select '正式工的最高年龄' ,Max(Fage) from T_employee
    union
    select '正式工的最低年龄' ,Min(Fage) from T_employee

    select round(3.1415926,3)
                 3.1420000

    函数

    流控函数
    select Fname,
    (
     case i //当判断条件的时候,case不能有值
     when 1 then '普通客户'
     when 2 then '会员'
     when 3 then 'vip'
     else '未知客户'
     end
    ) as 客户类型
       
    from T_Customer

    索引

    * 全表扫描:对数据进行检索,效率最差的是全表扫描
      为经常查询的字段查询建立索引,缺点降低insert,update,delete的执行效率

    [picPath] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,

    Jion



    1、select 1 与 select *的区别
        selelct 常量 from ... 对应所有行,返回的永远只有一个值,即常量 。所以正常只会用来判断是否有还是没有(比如exists子句)。而select * from ... 是返回所有行的所有列。
        性能上的差异,关键看你的from和where子句。比如说如果你的where条件中可以通过索引,那显然 select 1 from ... 的性能比 select * from ... 好。
    2、select count(1)与select count(*)的区别
       跟表结构有关系:
       如果表中没有主键,那么count(1)比count(*)快
       如果有主键,那么count(主键,联合主键)比count(*)快
       如果表中只有一个字段,count(*)最快
    3、select sum(1)的使用
       select count(*)返回所有满足条件的记录数,此时同select sum(1)
       但是sum()可以传任意数字,负数、浮点数都可以,返回的值是传入值n*满足条件记录数m


    //记得
    select caid from news  where caid  in(select id from category)
    select caid from news n where exists(select id from category c where c.id=n.caid)

    表A(小表),表B(大表)
    select * from A where cc in (select cc from B)
    效率低,用到了A表上cc列的索引
    select * from B where cc in (select cc from A)
    效率高,用到了B表上cc列的索引

    select * from A where exists(select cc from B where cc=A.cc)
    效率高,用到了B表上cc列的索引。
    select * from B where exists(select cc from A where cc=B.cc)
    效率低,用到了A表上cc列的索引。


    --分页要用到的
    select top 3 id from news  where id not in(select top 3 id from news order by id asc)

    select top 3 id from news  where not exists(select * from (select top 3 id from news order by id asc) T where T.id=news.id)


    select top 3 id from news where not exists(select* from(select top 3 id from news order by id asc) T where T.id=news.id)

  • 相关阅读:
    Java 使用对话框选择文件并输出到控制台
    Java 数组 可变长参数 实例
    Java 返回一个整数的各个数字之和的一种方法
    weblogic控制台部署web项目图解
    合理的饮食+适当的运动=健康的身体
    Linux远程管理器xshell和xftp使用教程,以及遇到关闭Xshell后项目也停止的解决方法
    java使用freemarker 生成word文档
    MyEclipse 2014优化设置(禁用myeclipse updating indexes)
    ORACLE中查询被锁定的表,以及如何解锁
    MySql查询优化
  • 原文地址:https://www.cnblogs.com/aiyp1314/p/2140448.html
Copyright © 2011-2022 走看看