SQL数据库语句2.docx
《SQL数据库语句2.docx》由会员分享,可在线阅读,更多相关《SQL数据库语句2.docx(11页珍藏版)》请在冰点文库上搜索。
![SQL数据库语句2.docx](https://file1.bingdoc.com/fileroot1/2023-8/5/13337f1f-a041-408c-8a2c-04ef41a27d72/13337f1f-a041-408c-8a2c-04ef41a27d721.gif)
SQL数据库语句2
--案例1:
查询作者不是金庸图书的作者,编号,类型,进货量
selectbid,author,tpe,numinputfrombook
whereauthor!
='金庸'
selectbid,author,tpe,numinputfrombook
whereauthor<>'金庸'
--案例2:
查询恐怖类所有图书的名字,作者
selectbname,authorfrombook
wheretype='恐怖类'
--案例3:
进货量超过10000的所有图书的名字,进货量,库存量
selectbaname,numinput,numstorefrombook
wherenuminput>10000
--案例4:
库存量小于等于5000的所有图书的信息
selectbid,bname,pub,author,type,numinput,numstorefrombook
wherenumstore<=5000
--2null:
表示该列的值不存在
--1)为空:
isnull
--2)不为空:
isnotnull
--案例:
查询作者为null,图书的名字,类型,作者名
selectbname,type,authorfrombook
whereauthorisnull
--案例:
查询类型不为null的图书的编号,名字,作者,出版社,类型
selectbid,bname,author,pub,typefrombook
wheretypeisnotnull
--3模糊查询:
like
--通配符:
--%:
代表任意的0个或者多个字符
--_:
代表任意的一个字符
--案例:
查询作者的姓“九”的所有图书的名字,作者,类型,出版社
selectbname,author,type,pubfrombook
whereauthorlike'九%'
--案例:
查询作者名字的最后一个字是明的所有图书的信息
select*frombook
whereauthorlike'%明'
--案例:
查询图书的倒数第二个字是“侠”的图书信息
select*frombook
wherebnamelike'%侠_'
--案例:
查询作者的名字倒数第3个字是”上“的图书信息
select*frombook
whereauthorlike'%上__'
--4逻辑运算符:
--and逻辑与or逻辑或not逻辑非
--案例:
图书的类型不是漫画类的所有的图书的名字,作者,类型
selectbname,author,typefrombook
wherenottype='漫画类'
--案例:
图书的类型是漫画类的且作者的名字最后一个字是“明”查询图书的所有信息
select*frombook
wheretype='漫画类'andauthorlike'%明'
--案例:
图书的出版社是海南出版社,或者图书类型是武侠类的图书信息
select*frombook
wherepub='海南出版社'ortype='武侠类'
--案例:
查询出版社为null,并且类型为null的所有图书的名字,作者
selectbname,authorfrombook
wherepubisnullandtypeisnull
--案例:
查询文学类或者作者是鸟山明的图书的名字作者类型
selectbname,author,typefrombook
wheretype='文学类'orauthor='鸟山明'
--案例:
查询类型不是漫画类并且作者不是黄易的图书的信息
select*frombook
wherenottype='漫画类'andauthor!
='黄易'
--案例:
进货量在10000~50000之间的所有图书的信息
select*frombook
wherenuminputbetween10000and50000
select*frombook
wherenuminput>=10000andnuminput<=50000
--案例:
库存量大于2000,并且进货量小于100000的图书的信息
select*frombook
wherenumstore>2000andnuminput<100000
--5between初值1and初值2
--范围:
初值1~初值2之间所有的数据
--案例:
查询库存量在2000~3000之间所有的图书信息
select*frombook
wherenumstore>=2000andnumstore<=3000
select*frombook
wherenumstorebetween2000and3000
--案例:
求出图书编号3~11的图书的名字,作者,类型,出版社
selectbname,author,type,pubfrombook
wherebidbetween3and11
--案例:
求出图书的进货量在50000~100000之间的所有图书的编号,名字,作者,类型,进货量
selectbid,bname,author,type,numinputfrombook
wherenuminputbetween50000and100000
--案例:
查询图书的编号是1,4,6,8,10的图书的信息
select*frombook
wherebid=1orbid=4orbid=6orbid=8orbid=10
--in:
指代多个同一列的值
--in(列值1,列值2,列值3...)
select*frombook
wherebidin(1,4,6,8,10)
--案例:
查询作者是黄易,金庸,曹雪芹,九把刀,郭敬明的所有图书的信息
select*frombook
whereauthorin('黄易','金庸','曹雪芹','九把刀','郭敬明')
--7排序:
根据某一个列或某几个列的数值,把查询结果进行排序
--1)升序,降序
--2)格式;
--select*/列名from表名
--where条件
--orderby列名asc(升序)/desc(降序)
--案例:
按照图书的进货量升序排列所有图书的信息
select*frombook
orderbynuminputasc
--案例:
按照图书的编号进行降序排列,显示图书的编号,书名,作者,出版社,类型
selectbid,bname,author,pub,typefrombook
orderbybiddesc
--3)默认升序排列
--案例:
按照图书的进货量升序排列所有图书的信息
select*frombook
orderbynuminput
--4)如果要排序的列的列值出现一致情况,可以指定其他的列作为排序标准
--案例:
按照图书的进货量的升序进行排列,如果进货量一致,则按图书编号降序排列
select*frombook
orderbynuminput,biddesc
--案例:
查询清华大学出版社,出版的所有的图书,按照库存量升序排列,显示书名,作者,出版社,库存量
selectbname,author,pub,numstorefrombook
wherepub='清华大学出版社'
orderbynumstore
--案例:
查询作者不为null而且进货量在20000~100000之间,根据进货量倒序显示图书编号,书名,作者,进货量,库存量,
--如果进货量一致,按照库存的升序排列
selectbid,bname,author,numinput,numstorefrombook
whereauthorisnotnullandnuminputbetween20000and100000
orderbynuminputdesc,numstore
--案例:
查询库存量在500以上并且类型不是漫画类,根据库存量进行升序排列,如果库存量一致,根据编号进行降序排列,
--显示图书的编号,名字,作者,类型,进货量,库存量
selectbid,bname,author,type,numinput,numstorefrombook
wherenumstore>500andtype!
='漫画类'
orderbynumstore,biddesc
--8聚合函数(分组函数)
--1)count:
统计数量
--a)count(*):
统计该表中所有数据的个数
selectCOUNT(*)数据条数frombook
--b)count(列名):
该列中所有列值的个数
selectCOUNT(bid)frombook
selectcount(author)frombook
selectcount(type)frombook
--案例:
查询图书种类的个数
selectcount(distincttype)frombook
--案例:
查询出版社的个数
selectcount(distinctpub)frombook
--2)sum(列名):
求和,把该列所有的列值都累加到一起
--案例:
查询进货量的总和
selectsum(numinput)总的进货量frombook
--3)avg(列名):
平均数
--案例:
求出所有的图书的平均值
selectavg(numinput)frombook
--案例:
求出小说类所有图书的平均库存量,及库存总量
selectavg(numstore)平均库存量,sum(numstore)库存总量frombook
wheretype='小说类'
--4)max(列名):
该列的所有列值中,最大的值
--案例:
查询库存量最大的图书的库存量
selectmax(numstore)frombook
--查询清华大学出版社和海南出版社中所有图书进货量的最大值
selectmax(numinput)frombook
wherepub='清华大学出版社'orpub='海南出版社'
--5)min(列名):
该列的所有列值中,最小的值
--案例:
求出小说类图书中最小的进货量
selectmin(numinput)frombook
wheretype='小说类'
--9分组:
根据某个列把所有的数据分成几组,然后再使用聚合函数
--1)格式:
--select列名/聚合函数from表名
--where条件
--groupby列名
--orderby列名asc/desc
--2)分组套路:
--步骤1:
where条件
--步骤2:
根据列名把表中所有的数据分组
--步骤3:
考虑select后跟列名,列名要么出现在groupby后面,(或者出现在聚合函数)
--案例:
求出每个出版社图书的进货量的最大值,最小值,平均值,并显示出版社的名称
selectmax(numinput)进货量最大值,min(numinput)进货量最小值,avg(numinput)进货量平均值,
pub
frombook
groupbypub
--求出每个出版社库存量的最大值,平均值,并且按照库存量的平均值进行降序排列
--显示各个出版社的名字
selectmax(numstore)库存量最大值,avg(numstore)库存量平均值,pub
frombook
groupbypub
orderbyavg(numstore)desc
selectmax(numstore)库存量最大值,avg(numstore)库存量平均值,pub
frombook
groupbypub
orderby库存量平均值desc
--案例:
查询作者不null的每种类型的图书库存量的最大值,最小值,类型名称,按照库存量的最大值进行升序排列
selectmax(numstore)库存量最大值,min(numstore)库存量最小值,typefrombook
whereauthorisnotnull
groupbytype
orderbymax(numstore)
--案例:
查询进货量在10000~90000之间所有的图书,每种类型图书最大的库存量,平均的库存量,图书的个数,类型名称
--按照平均的库存量进行升序排列
selectmax(numstore)最大库存量,avg(numstore)平均库存量,count(*)图书个数,typefrombook
wherenuminputbetween10000and90000
groupbytype
orderbyavg(numstore)
--案例:
出版社不能null并且type不能null,求出每个出版社进货的总量,平均值,最小值
--按照进货总量进行升序排列,如果进货总量一致,按照平均值降序排列
selectsum(numinput)进货的总量,avg(numinput)平均值,min(numinput)最小值,pubfrombook
wherepubisnotnullandtypeisnotnull
groupbypub
orderbysum(numinput),avg(numinput)desc
--10限定返回数据的条数
--1)限制返回的固定的行数
--selecttopn列名/*from表名where条件
--案例:
查询book表中所有的数据,返回前3条数据
selecttop3*frombook
--案例:
查询book表中书名,出版社,作者,要求作者不为null,类型不为null,返回前5条数据
selecttop5bname,pub,authorfrombook
whereauthorisnotnullandtypeisnotnull
--2)按百分比返回数据
--selecttopnpercent*/列名from表名where条件
--案例:
查询所有的图书,出版社不能为null,返回50%的数据
selecttop50percent*frombook
wherepubisnotnull
--案例:
查询所有的图书,作者不为null,返回25%的数据
selecttop25percent*frombook
whereauthorisnotnull
--11select语句来复制表(不能复制主键,需要在复制表的外边来指定主键)
--1)把表中所有的数据,完全复制过去
--select*into新表名from源表名
select*intobook2frombook
--2)指定对应的列复制
--select列名1,列名2···into新表名from源表名
selectbid,bname,type,authorintobook3frombook
--案例:
创建一个新的表book4,包含book表中bid,bname,type,进货量及库存量
selectbid,bname,type,numinput,numstoreintobook4frombook
select*frombook4
--3)制定的条件,复制对应的数据
--select列名/*into新表名from源表名where条件
--案例:
创建book5,需要包含book表中bid,bname,type,pub,author,并且所有的数据必须包含所有字段的值
selectbid,bname,type,pub,authorintobook5frombook
wherebnameisnotnullandtypeisnotnullandpubisnotnullandauthorisnotnull
select*frombook5
--4)只复制表的结构,不复制表中的数据
--where条件必须为假
--select列名/*into新表名from源表名
--where条件(假)
select*intobook6frombook
where1!
=1