• [SQL server]常用SQL(一)


    1.在Select语句中使用判断查询:
    功能:计算条件列表并返回多个可能结果表达式之一。
    示例:以判断user_pass字段值是否为空,为空时值为yes反之为no查询数据,条件为user_name不为空
    select
     case
      when user_pass is null then 'yes' else 'no'
     end as 'user_pass'
     ,user_name as 'admin'
    from
     admin
    where
     user_name is not null

    ------------------------------------

    2.datepart函数的使用
    功能:返回代表指定日期的指定日期部分的整数。
    示例:查询2004年与2005年之间的数据
    select * from admin
    where datepart( yyyy,date_time )
     between 2004 and 2005

    ------------------------------------

    3.datediff函数使用
    功能:返回跨两个指定日期的日期和时间边界数。
    示例:打印日期差
    declare @date_time datetime
    set @date_time = convert( datetime,'2005-05-06' )
    print datediff( dd,@date_time,getdate() )


    ------------------------------------

    4.exists关键字使用
    功能:指定一个子查询,检测行的存在。
    示例1:判断用户'admin'是否存在,如存在就返回所有行。
    select *
    from
     admin
    where
     exists( select user_pass from admin where user_name='admin' )

    示例2:判断用户'admin'是否存在,如不存在就返回所有行。
    select *
    from
     admin
    where
     not exists( select user_pass from admin where user_name='admin' )


    ------------------------------------

    5.@@IDENTITY关键字
    功能:返回最后插入的标识值。
    示例:插入一新行,打印插入的新行的标识ID值。
    insert admin( user_name,user_pass,date_time,team_group )
    values
     ( 'test','test',getdate(),3 )
    print @@identity


    ------------------------------------

    6.@@rowcount关键字
    功能:返回受上一语句影响的行数。
    示例1:选择数据,返回所选择的数据的行数
    select * from admin
    print @@rowcount
    示例2:更新数据,返回被更新数据所影响的行数
    update admin set user_name='test' where user_name='zxb'
    print @@rowcount


    ------------------------------------

    7.Group by应用
    功能:
    示例1:
     SQL查询:select type,price from titles where royalty = 10
     结果:
      type  price
      -----------------------
      business     19.9900
      business     11.9500
      business     19.9900
      popular_comp 20.0000
      psychology   21.5900
      psychology   7.0000
      psychology   19.9900
      psychology   7.9900
      trad_cook    20.9500
      trad_cook    14.9900
     Group by 分组查询:select type,sum(price) as price from titles where royalty=10 group by type
     结果:
      type  price
      -----------------------
      business     51.9300
      popular_comp 20.0000
      psychology   56.5700
      trad_cook    35.9400
     Group by all 分组查询:select type,sum(price) as price from titles where royalty=10 group by all type
     结果:
      type  price
      -----------------------
      business     51.9300
      mod_cook     NULL
      popular_comp 20.0000
      psychology   56.5700
      trad_cook    35.9400
      UNDECIDED    NULL

  • 相关阅读:
    20155313 杨瀚 《网络对抗技术》实验九 Web安全基础
    20155313 杨瀚 《网络对抗技术》实验八 Web基础
    20155313 杨瀚 《网络对抗技术》实验七 网络欺诈防范
    20155313 杨瀚 《网络对抗技术》实验六 信息搜集与漏洞扫描
    20155313 杨瀚 《网络对抗技术》实验五 MSF基础应用
    20155313 杨瀚 《网络对抗技术》实验四 恶意代码分析
    20155313 杨瀚 《网络对抗技术》实验三 免杀原理与实践
    20155313 杨瀚 《网络对抗技术》实验二 后门原理与实践
    20155313 杨瀚 《网络对抗技术》实验一 PC平台逆向破解(5)M
    20155313 2017-2018-1 《信息安全系统设计基础》课程总结
  • 原文地址:https://www.cnblogs.com/HD/p/266942.html
Copyright © 2020-2023  润新知