指令
1. 分组 -- group by (类似excel筛选同类目,相同的合并)
select 分组列名 from 表名 group by 分组列名
举例 如下
2.分组之后
使用聚合函数 avg() sum() count() max() min()
再进行筛选 having
--select 分组列名 from 表名 group by 列名 having 过滤条件
案例如下 (分单组)
-- 每轮所有成绩都及格的学生 (分多组)
select 姓名 from student group by 姓名,轮次 having min(成绩)>60
3.嵌套
信息在两张表里
4.连接
练习
select customername,balance from userInfo left join cardInfo on userInfo.customerID=cardInfo.customerID where customername = '丁强'
select * from userInfo RIGHT JOIN cardInfo on userInfo.customerID=cardInfo.customerID
1)查询用户刘流的余额是多少?
select balance,customername from userInfo left join cardInfo on userInfo.customerID =userInfo.customerID where customername = '刘流'
2)查询手机号是135开头的客户号和银行卡余额
select balance,u.customerid from userInfo u left join cardInfo c on u.customerid = c.customerid where telephone like '135%'
3)查询开户金额大于500的用户信息
select * from userInfo u left join cardInfo c on u.customerid = c.customerid where openmoney>500
4)查询卡号是1130结尾的且交易金额大于100银行卡密码是多少
select password from cardInfo c left join tradeInfo t on c.cardid = t.cardID where t.cardid like '%1130' and transmoney >100
1)查询交易总额超过10000的有哪些日期
select substr(transdate,1,10) from tradeInfo group by substr(transdate,1,10) having sum(transmoney)>10000
2)查询2021年9月份中交易金额超过10000的有哪些日期
select substr(transdate,1,10) from tradeInfo where SUBSTR(transdate,1,7) =2021-09 group by substr(transdate,1,10) having sum(transmoney)>10000
3)查询交易总额超过10000的月份
select substr(transdate,1,7) from tradeInfo GROUP BY substr(transdate,1,7) having sum(transmoney)>10000
4)查询不同的存款类型的银行卡余额最大值大于500 的银行卡卡号和余额
select cardid ,balance from cardInfo where savingid in (select savingid from cardInfo GROUP BY savingid having max(balance)>500)
1)查询存入和支取的总额分别是多少
select transtype,sum(transmoney) from tradeInfo GROUP BY transtype
2)查询存入和支取的最大值分别是多少
select transtype,max(transmoney) from tradeInfo GROUP BY transtype
3)查询不同存款类型的银行卡--开卡金额的平均值
select avg(openmoney) from cardInfo GROUP BY savingid
4)查询尾号为1130这张银行卡的交易总额和交易类型
select sum(transmoney) ,transtype from tradeInfo where cardid like '%1130' group by transtype