zoukankan      html  css  js  c++  java
  • 查询select

    --------------select查询-----------------
    --查询所有信息(方法一)
    select * from stuinfo --*号代表所有列
    --查询所有信息(方法二)
    select StuNo,StuName,StuAge,StuSex,address from stuinfo
    --查询单列
    select StuName from stuinfo
    --查询多列
    select StuNo,StuName from stuinfo
    --将多列组合成一列
    select StuName+'_'+address as 姓名_地址 from stuinfo

    --查询中为列名取别名
    --方法一
    select StuNo as 学号,StuName as 姓名 from stuinfo
    --方法二
    select StuNo 学号,StuName 姓名 from stuinfo
    --方法三
    select 学号=StuNo,姓名=StuName  from stuinfo

    --查询值空的行
    select * from StuInfo where address is NULL
    --注意:判断为空用 is NULL而不是 =NULL
    --查询值不为空的行
    select * from StuInfo where address is not NULL

    --常量列
    select 问候='你好'
    select 21.5
    select 学号=StuNo,姓名=StuName,学院='硅谷学院' from stuinfo

    --topN查询(限制查询行显示结果)
    --以行来筛选
    select top 2 * from StuInfo where StuAge>21
    --以百分比来筛选
    select top 25 percent * from StuInfo

    --消除重复行查询
    select distinct StuSex from StuInfo--消除性别重复的行
    select distinct StuName,StuSex from StuInfo--消除性别和姓名都重复的行

    --between查询条件(判断某个区间段)
    select * from StuInfo where StuAge between 21 and 23
    --注意:StuAge between 21 and 23相当于StuAge>=21 and StuAge<=23

    --in查询条件(判断是否在指定的集合中)
    select * from StuInfo where StuAge in(21,23)

    --and条件查询(左右两边的条件为真结果才为真)
    select * from StuInfo where StuName='张三' and StuAge=21

    --or条件查询(左右两边的条件只要有一个为真结果就为真)
    select * from StuInfo where StuName='张三' or StuAge=21

    --模糊查询
    select * from StuInfo where StuName like '张%'

    ---------------order by排序---------------------
    --升序(asc 默认)
    select * from StuInfo order by StuAge
    --注意:排序中如果没有加asc、desc,默认就为asc
    --降序(desc)
    select * from StuInfo order by StuAge desc
    --多字段排序
    select * from StuScore order by score desc,StuNo desc
    --注意:多字段排序时,会先对第一个字段进行排序,如果第一个字段无法排序的行就使用第二个字段进行排序,以此类推...

    --只能查出一行数据
    select top 1 * from StuInfo order by StuAge
    --with ties能显示并列的行
    select top 1 with ties * from StuInfo order by StuAge

    ------------------分组查询---------------------
    --查询每门课程的总分(课程名,总分)
    select subject,sum(score) from
    StuScore group by subject
    --查询每位同学的总分(学号,总分)
    select StuNo,sum(score) from
    StuScore group by StuNo
    --注意:分组查询中,字段能够出现在select后面的情况:
    --1、该字段有出现在group by后面
    --2、该字段出现在聚合函数中
    --3、常量列
    select StuNo,sum(score),'sve' as 常量列 from
    StuScore group by StuNo

    --查询每位学员有进行几次考试
    select StuNo,count(*) from StuScore
    group by StuNo
    --查询出没有缺考的学号
    select StuNo,count(*) from StuScore
    group by StuNo having count(*)>2
    --查询出没有缺考并且有通过考试的学号
    select StuNo,count(*) from StuScore
    where score>=60
    group by StuNo having count(*)>2
    order by StuNo desc
    --关键字顺序:where->group by->having->order by
    --where:先对数据集进行条件筛选
    --group by:对剩下来的数据进行分组
    --having:对分组后的结果再次进行筛选
    --order by:进行排序

    ----------------多字段分组---------------------
    --查询有参加补考的学号、科目、考试次数
    select StuNo as 学号,subject as 科目,count(*) as 考试次数 from StuScore
    group by StuNo,subject having count(*)>1


    ----------------修改卡密码---------------------
    --方法一
    update Card set PassWord=replace(PassWord,'o','0')
    update Card set PassWord=replace(PassWord,'i','1')
    --方法二
    update Card set PassWord=replace(replace(PassWord,'o','0'),'i','1')

    ------------------排序---------------------
    select left(num,charindex('-',num,1)-1) from Card
    select right(num,len(num)-charindex('-',num,1)) from Card
    --方法一
    select * from Card order by  
    convert(int,left(num,charindex('-',num,1)-1)),
    convert(int,right(num,len(num)-charindex('-',num,1)))
    --方法二
    select * from Card order by  
    convert(int,left(num,charindex('-',num,1)-1)),
    convert(int,stuff(num,1,charindex('-',num,1),''))

  • 相关阅读:
    mysql"ON DUPLICATE KEY UPDATE"的用法
    shell 数组用法
    linux命令行提示符显示太长怎么办?
    热备份、温备份、冷备份(Hot/Warm/Cold Backup)
    Domain key在反垃圾邮件中的应用
    计算机的存储单位
    IIS W3C日志记录字段和HTTP状态代码的说明
    noarch
    日志传送
    Remote Desktop Issues
  • 原文地址:https://www.cnblogs.com/danmao/p/3870760.html
Copyright © 2011-2022 走看看