• SQL基础2 Carl


      1 --聚合函数MAX(最大值)、MIN(最小值)、AVG (平均值)、SUM (和)、COUNT(数量:记录的条数。)
      2 
      3 --查询数学成绩中最高分是多少分
      4 select max(fMath) as 数学成绩最高分 from MyStudent
      5 
      6 --求总分
      7 select sum(fMath) as 总分 from MyStudent
      8 
      9 --求平均分(对空值不处理)
     10 select avg(fMath) as 平均分 from MyStudent
     11 
     12 --求班级中总的记录条数(总人数)也不考虑空值
     13 select count(*) as 总人数 from MyStudent
     14 
     15 
     16 -----------多条件查询----------
     17 
     18 --查询年龄在20--30岁之间的男学生
     19 select
     20 *
     21 from MyStudent
     22 where age between 18 and 30 and gender=''
     23 ---between 18 and 30 也相当于是>=18 and <=30
     24 
     25 --查询班级id为1 2 3的所有学生
     26   select * from Student
     27   where ClassId=1 or ClassId=2 or ClassId=3
     28 --可以用in()替代多个or
     29   select * from Student
     30   where ClassId in(1,2,3)
     31   
     32 ------------模糊查询------------
     33 
     34 --查询出所有姓‘赵’的同学
     35 --通配符%表示;任意多个任意字符
     36 select * from MyStudent where name like '赵%'
     37 
     38 --查询出姓名中包含‘敏’字的同学
     39 select * from MyStudent where name like '%敏%'
     40 
     41 --查询出所有姓‘赵’的同学,并且姓名是三个字的
     42 --通配符_ 表示任意的单个字符
     43 select * from MyStudent where name like '赵__'
     44 
     45 --查询出姓名中包含‘赵’或‘云’的人的姓名 
     46 --通配符[]表示括号中的任一个字符 只选一个匹配
     47 select * from MyStudent where name like '[赵云]'
     48 
     49 --表示X与Y之间只要不是'赵'或‘云’的任意单个字
     50 --[^]表示除了中括号中的任意单个字符
     51 select * from MyStudent where name like '[^赵云]'
     52 
     53 --查询姓名中包含%的
     54 --用[]括起来,就表示转义
     55 select * from MyStudent where name like '[%]'  
     56 
     57 --查询出所有不姓赵的同学
     58 select * from MyStudent where name not like '赵%'
     59 
     60 --查询出学生成绩中数学成绩为空的人
     61 --null在数据库中表示unkonw(不知道),判断一个值是否为null,也就不能用=或者<>来判断
     62 select * from MyStudet where Math=null
     63 
     64 --null与null比较结果还是null(null就表示不知道,"不知道"在where中就认为是false,所以不返回任何数据)
     65 select * from MyStudent where Math<>null  
     66 
     67 --查询所有math为非null的值
     68 select * from MyStudent where Math id not null
     69 
     70 
     71 select * from tblstudent group by TSGENDER
     72 --请从学生表中查询出每个班的班级Id和班级中男同学的人数: 
     73 select
     74   tsclassid as 班级id,
     75   count(*) as 班级人数
     76 from TblStudent 
     77 where tsgender=''
     78 group by tsclassid 
     79 --一般分组语句group by 都要与聚合函数配合使用,如果没有聚合函数,分组的意义不大。
     80 
     81 --当在select查询语句中出现聚合函数时,这时不能在select查询中再出现其他列,除非:该列也在group子句中出现或者也在聚合函数中出现。
     82 
     83 
     84 
     85 --请从学生表中查询出每个班的班级Id和班级中男同学的人数并且班级人数大于2:
     86 select
     87   tsclassid as 班级id,
     88   count(*) as 班级人数
     89 from TblStudent 
     90 where tsgender=''
     91 group by tsclassid 
     92 having count(*)>2
     93 
     94  --having语句后能跟什么列,主要看:分组后得到的结果集中包含什么列。
     95  
     96 ---------执行的顺序------
     97 select 
     98     --distinct / top 之类的关键字(这些都是一些现实的选项)
     99     fgender as 性别,  --5>选择列
    100     count(*) as 人数
    101 from MyStudent --1>先从MyStudent表中拿到数据(全部数据的一个结果集)
    102 where fage>30  --2>从MyStudent的数据中筛选出所有年龄大于30岁的人的信息(新结果集,都是年龄大于30的)
    103 group by fgender --3>按照性别分组,分完组以后又得到一个新结果集(分组后的结果)
    104 having count(*)>355 --4>基于分组以后的结果集,然后在筛选,筛选出那些组中记录大于500的组。
    105 order by  人数 desc--6>最后把显示出来的结果排序
    106 
    107 
    108 --------------类型转换---------------
    109 --select 100+'hello'
    110 select cast(100 as varchar(10))+'hello'
    111 
    112 select convert(varchar(10),100)+'hello'
    113 
    114 select convert(varchar(50),getdate())
    115 select convert(varchar(50),getdate(),101)
    116 select convert(varchar(50),getdate(),100)
    117 select convert(varchar(50),getdate(),111)
    118 select convert(varchar(10),getdate(),126)
    119 
    120 
    121 
    122 ----------------联合----------------------
    123 --union 的作用就是将多个结果集并到了一起
    124 select tsName,tsAge from tblstudent
    125 union all
    126 select TTName,tTAge from DATA.dbo.TblTeacher
    127 
    128 --联合要注意的地方
    129 --1.多个结果集中的列的数据类型必须一一对应
    130 --2.列的个数必须一样
    131 --联合的时候如果只写union 则会去除重复数据,如果写unoin all 则不会去除重复的数据
    132 
    133 
    134 --假设有个销售表,请查询出每个销售员的销售总金额以及总的销售金额
    135 --要求:总的销售金额显示在表的底部
    136 select 
    137     销售员,
    138     销售金额=sum(销售数量*销售价格)
    139 from MyOrders
    140 group by 销售员
    141 union all
    142 select '总销售额:',sum(销售价格*销售数量)
    143 from MyOrders
    144 
    145 
    146 
    147 -----------字符串函数----------------
    148 select len('长度abc') --返回字符的个数   5
    149 select datalength('长度abc')  --返回是字节的个数 7
    150 select lower('Abc')  --返回abc
    151 select upper('Abc')  --返回ABC
    152 
    153 --LTRIM():字符串左侧的空格去掉 
    154 --RTRIM () :字符串右侧的空格去掉 
    155 
    156 select '==========='+rtrim(ltrim('     aaa       '))+'===============' 
    157 --返回===========aaa===============
    158 
    159 --LEFT()、RIGHT()  截取取字符串
    160 select left('abcdefg',2)  --返回ab
    161 select right('abcdefg',2) --返回fg
    162 
    163 --SUBSTRING(string,start_position,length),索引从1开始。
    164 select substring('abcdefg',2,3)  --返回bcd
    165 
    166 
    167 
    168 ---------- 日期函数------------
    169 --getdate 取得当前日期时间 
    170 select getdate() --2012-08-23 18:09:00.540
    171 --DATEADD (datepart , number, date ),计算增加以后的日期。参数date为待计算的日期;参数number为增量
    172 --参数datepart为计量单位
    173 
    174 select dateadd(month,2,getdate())--2012-10-23 18:09:13.527
    175 
    176 --DATEDIFF ( datepart , startdate , enddate ) :计算两个日期之间的差额。 datepart 为计量单位
    177 select datediff(second,'2012-08-23 18:09:00.540','2012-8-23 18:09:17.527') --17 minute 分
    178 
    179 --DATEPART (datepart,date):返回一个日期的特定部分
    180 --Month()、year()、day()来代替
    181 select year(getdate())  --2012
  • 相关阅读:
    Go知识点大纲
    Golang GC(垃圾回收机制)
    ubuntu20.04安装EasyConnect兼容性问题解决
    ES常用操作
    Golang使用validator进行数据校验及自定义翻译器
    ubuntu下安装navicat破解版
    GORM基本使用
    Golang之指针
    drf的核心:序列化模块之Serializer和ModelSerializer(重点)模块
    Django中 media资源配置
  • 原文地址:https://www.cnblogs.com/sc0791/p/2653068.html
Copyright © 2020-2023  润新知