SELECT MONTH(transDate),DAY(transDate) FROM tradeInfo ;
-- 7)查询丁强和王旺两个客户手机号后4位
SELECT SUBSTR(userInfo.telephone,7,4) FROM userInfo WHERE userInfo.customerName IN("丁强","王旺");
-- 1)查询交易金额最高的银行卡号(用limit)
SELECT tradeInfo.cardID FROM tradeInfo ORDER BY transMoney DESC LIMIT 1;
-- 2)查询存款余额前三名的银行卡信息
SELECT * FROM cardInfo ORDER BY cardInfo.balance DESC LIMIT 3;
-- 3)查询做的支取交易,按照交易金额排序,最小的三个银行卡号
SELECT cardID FROM tradeInfo WHERE tradeInfo.transType = '支取' ORDER BY transMoney LIMIT 3;
-- 4)查询交易信息表中,存入金额是第2高到第5高的交易
SELECT * FROM tradeInfo WHERE transType = '存入' ORDER BY transMoney DESC LIMIT 1,4;
复制代码
分组 对某一类分组 group by 配合函数一起用
只能查询体现到分组的那一组
-- 对存取类型transType 分组,只能查看分组类型这一列
SELECT transType FROM tradeInfo GROUP BY transType;
-- 查询每张卡交易的最大金额、最小金额
SELECT cardID,MAX(transMoney),min(transMoney) FROM tradeInfo GROUP by tradeInfo.cardID;
-- 查询存入交易的总金额
SELECT cardID,SUM(transMoney) FROM tradeInfo WHERE transType = '存入' GROUP by tradeInfo.cardID;
-- 查询2023-05-06日支取交易金额的最大值和和最小值 先筛,再分
-- WHERE transDate like '2023-05-06%' 也可以
SELECT cardID,MAX(transMoney),MIN(transMoney)FROM tradeInfo WHERE SUBSTR(transDate,0,10) ='2023-05-06' AND transType ='支取' GROUP by cardID;
复制代码
先分组,再进行筛选 group by 列名 having 过滤条件
-- 所有成绩都及格的学生
SELECT stuName FROM stu GROUP by stuName HAVING min(score) >= 60;
-- 没有学生不及格的科目
SELECT 科目 FROM stu GROUP by 科目 HAVING score >= 60;
-- 成绩最低都是80 的学生
SELECT stuName FROM stu GROUP by stuName HAVING min(score) >= 80;
复制代码
多重分组 GROUP by x,x2
-- 每轮所有成绩都及格的学生 先以姓名分组,再以轮次分组
SELECT 姓名 FROM stu GROUP by 姓名,轮次 HAVING min(score) >= 60;
复制代码
多表联查 嵌套
-- 1)查询交易金额大于2000的用户姓名和手机号
-- 方法1
-- SELECT userInfo.cardID FROM userInfo WHERE userInfo = (SELECT cardID FROM tradeInfo WHERE tradeInfo.transMoney > 2000)
-- 方法2
SELECT customerName,telephone FROM cardInfo,userInfo,tradeInfo WHERE cardInfo.customerID = userInfo.customerID AND cardInfo.cardID = tradeInfo.cardID AND tradeInfo.transMoney > 2000;
-- 2)查询支取交易金额大于400的用户姓名和手机号
SELECT customerName,telephone FROM cardInfo,userInfo,tradeInfo WHERE cardInfo.customerID = userInfo.customerID AND cardInfo.cardID = tradeInfo.cardID AND transType = '支取' AND transMoney >400;
-- 查询账户余额大于5000的客户姓名
SELECT userInfo.customerName FROM userInfo WHERE userInfo.customerID = (SELECT customerID FROM cardInfo WHERE cardInfo.balance > 5000);
-- 存款方式为1的账户的交易金额
SELECT transMoney FROM tradeInfo WHERE tradeInfo.cardID IN (SELECT cardID FROM cardInfo WHERE cardInfo.savingID =1);
-- 4)查询不同的存款类型的银行卡余额最大值大于500的银行卡卡号和余额
select cardID,balance from cardInfo where savingid in (select savingid from cardInfo GROUP BY savingid having max(balance)>500);
复制代码
连表 左毗连 右毗连
左毗连称之为左外毗连 右毗连称之为右外毗连 这俩个毗连都是属于"外毗连" 左毗连关键字:left join 表名 on 条件 / left outer 表名 join on 条件
右毗连关键字:right join 表名 on 条件/ right outer 表名 join on 条件
左毗连说明:left join 是left outer join的简写,左(外)毗连,左表(a_table)的记录将会全部表示出来, 而右表(b_table)只会体现符合搜刮条件的记录。右表记录不敷的地方均为NULL。
右毗连说明:right join是right outer join的简写,与左(外)毗连相反,右(外)毗连,左表(a_table)只会体现符合搜刮条件的记录,而右表(b_table)的记录将会全部表示出来。左表记录不敷的地方均为NULL。
下方为左毗连的体现
SELECT * FROM cardInfo left join tradeInfo on cardInfo.cardID = tradeInfo.cardID ;
复制代码
练习
-- 3)查询交易总额超过10000的月份
SELECT month(transDate) FROM tradeInfo group by month (transDate) having SUM(transMoney) > 10000;
-- 2)查询手机号是135开头的客户号和银行卡余额
SELECT cardInfo.customerID,cardInfo.balance FROM cardInfo WHERE cardInfo.customerID IN (SELECT customerID FROM userInfo WHERE userInfo.telephone LIKE '135%');
-- 1)查询存入和支取的总额分别是多少
SELECT transType,SUM(tradeInfo.transMoney) FROM tradeInfo GROUP by tradeInfo.transType;
复制代码
增长字段
insert into 表名 values (值1,值2,值3··) 除了自增,其他的必须全部字段名全都输入
insert into 表名(列名,列名2,列名3··) values (值1,值2,值3··) 字段名对应值,逐一对应
INSERT INTO userInfo VALUES(6,"王佳",675365343497420574,16543268749,"湖北荆门");
-- Column count doesn’t match value count at row 1 意思是列数目跟值数目不匹配。
复制代码
修改表
-- UPDATE userInfo SET customerName = '新值' WHERE customerName = '旧值'
UPDATE userInfo SET customerName = '王天' WHERE customerName = '王佳'