SQL_SELECT语句

1、什么是SQL?

结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。

2、查询语句(select)的基本语法结构!

select 字段1,字段2,字段3....字段n
from 表名
where 查询条件
order by 排序字段 注意:asc升序排列(默认),desc代表降序

2.1 按照条件查询!

-- 1.查询天数在3-5天内的影片信息
select *
from north_american_box_office
where shangyingtianshu >= 3 and shangyingtianshu <=5

select *
from north_american_box_office
where shangyingtianshu between 3 and 5

-- 2.查询某一天上映的影片信息
select *
from north_american_box_office
where riqi = 20190801

-- 3.查询null值(累计票房为NULL)
select *
from north_american_box_office
where leijipiaofang is null

2.2 模糊查询!

  • 模糊查询条件值中,需要使用通配符
    % : 代表任意长度的字符
    _ : 代表1个长度的任意字符
-- 查询"速度与激情"开头的所有影片
select *
from north_american_box_office
where zhongwenpianming like '速度与激情%'

-- 查询"速度与激情"开头并且只包含6个字符长度的所有影片
select *
from north_american_box_office
where zhongwenpianming like '速度与激情_'

2.3 排序!

 -- 查询速度与激情系列电影,并按照累计票房降序排列
select *
from north_american_box_office
where zhongwenpianming like '速度与激情%'
order by leijipiaofang desc -- 降序排列

-- 按照累计票房降序排列,当累计票房一样的时候按照当日票房升序排列。
select *
from north_american_box_office
where zhongwenpianming like '速度与激情%'
order by leijipiaofang desc,dangripiaofang asc;

2.4 列表查询!

-- 查询北京市、广州市、长沙市、上海市的车牌信息
select *
from License_plate_number
where chengshi in( '北京市','广州市','长沙市','上海市' )

-- 查询除北京市、广州市、长沙市、上海市四个城市外的车牌信息
select *
from License_plate_number
where chengshi not in( '北京市','广州市','长沙市','上海市' )

2.5 限制查询数量!

使用limit关键字

-- 查询北美上映电影累计票房在1000至5000之间(只取前1000条数据)
select *
from north_american_box_office
where leijipiaofang between 1000 and 5000
limit 1000

-- 查询北美上映电影累计票房TOP10排行榜
select *
from north_american_box_office
order by leijipiaofang desc
limit 10

2.6 聚合查询!

聚合操作:求和 sum()、平均值avg()、最大值max()、最小值min()、个数统计count()

-- 统计指定电影的上映期间的累计票房和、平均票房
select sum(leijipiaofang),avg(leijipiaofang)
from north_american_box_office
where zhongwenpianming like'%速度与激情%'

-- 统计指定电影的最长上映天数,最短上映天数
select max(shangyingtianshu),min(shangyingtianshu)
from north_american_box_office
where zhongwenpianming like '%速度与激情%'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值