SELECT * FROM 家計簿 where 出金額 = max(出金額); 是无法运行的,因为存在组函数;
select max(出金額)from 家計簿;这里再求出最大值是多少;
将上面两个code合并,利用子查询,得出【出金额】最大值的那条完整数据【*】
SELECT * FROM 家計簿 where 出金額 = (select max(出金額)from 家計簿);
先执行括号内的select 文,再执行外侧的SQL文。
单一的值作为子查询
select max(出金額)from 家計簿
update 家計簿集計 set 平均 = (select avg (出金額)from 家計簿アーカイブ where 費目 ='食費') where 費目 ='食費';
select * from 家計簿アーカイブ where 費目 ='食費';
select 合計 from 家計簿集計 where 費目 ='食費';
select *,(select 合計 from 家計簿集計 where 費目 ='食費')as 过去的合计金额 from 家計簿アーカイブ where 費目 ='食費';
复数的值作为子查询 一般用in()来指定范围
select 出金額 from 家計簿
select * from 家計簿 where 費目 in(select 費目 from 家計簿集計);
Any 比任意一个值小
select * from 家計簿 where 費目 = '食費' and 出金額 < any (select 出金額 from 家計簿アーカイブ where 費目 = '食費' );
整个表作为子查询
select * from 家計簿
将1插入到アーカイブ 中 insert into 家計簿アーカイブ
则 insert into 家計簿アーカイブ (select * from 家計簿);
上面的inser into后面存在子查询,不能使用valus,但是可以省略后面的括号。
删除表内所有数据
delete from 家計簿
truncate table 家計簿
在from句子中使用子查询。必须起别名。
select sum(出金額) from (select * from 家計簿 union select * from 家計簿アーカイブ)as sub;
select sum(sub.出金額) from (select * from 家計簿 union select * from 家計簿アーカイブ)as sub; /同上