zoukankan      html  css  js  c++  java
  • [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

  • 相关阅读:
    Kubernetes 升级过程记录:从 1.17.0 升级至最新版 1.20.2
    歼10:职业有个基本属性,为了糊口。然后再区分 主动 or 被动,被动者只是为了糊口,那自然是看不起自己的职业
    红胖子:写给人生的九封信,愿你的人生淡定从容,繁华似锦!!!
    Embarcadero MVP(68位,全部都有个人主页)
    Delphi 26 岁
    C# Assembly 与 Reflection
    你精通那么多技术,为何还是做不好一个项目
    Linux 6 个“吓人”的 命令
    Distributed Application runtime
    RocketMQ and kafka
  • 原文地址:https://www.cnblogs.com/HD/p/266942.html
Copyright © 2011-2022 走看看