SQL语句简单查询

更新时间:2023-11-13 10:16:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

--模糊查询

--5、查询所有梁姓家庭成员(like)

select * from tb_users where uname like '梁%'

--6、查询日常收支表中所有rmenu即备注含‘去’字的记录

select * from tb_inoutinfo where rmenu like '%去%'

--7、查询日常收支表中2月到3月之间的收支情况(between)

select * from tb_inoutinfo where month(rdate) between 2 and 3

--8、查询日常收支表中1000到5000之间金额的收支记录

select * from tb_inoutinfo where rmoney between 1000 and 5000

--9、查询日常收支表中工资和奖金记录,即xid为1,2的记录

select * from tb_inoutinfo where xid in(1,2)

--聚合函数

--10、求存款额,即对金额求和

select sum(rmoney) as 总收入 from tb_inoutinfo where rmoney>0

--11、求总支出,即为金额为负数的求和

select sum(rmoney) as 总支出 from tb_inoutinfo where rmoney<0

select * from (select sum(rmoney) as 总收入 from tb_inoutinfo where rmoney>0 )a join (select sum(rmoney) as 总支出 from tb_inoutinfo where rmoney<0)b on 1=1

--12、求梁山伯今年发了几次工资,即为uid为1且xid为1的记录记数

select * from tb_inoutinfo where uid=1 and xid=1

--13、最大收入额,即求最大值

select max(rmoney) from tb_inoutinfo

--分组

--14、求每个人的财务金额和,即根据uid分组后求rmoney的和

select uid,sum(rmoney) from tb_inoutinfo group by uid

--15、求每个人的支出金额,即条件为rmoney为负数,根据uid分组求rmoney的和

select uid,sum(rmoney) from tb_inoutinfo where rmoney<0 group by uid

--16、求每个人的收入金额,但只显示超过10000元,即条件为rmoney大于0, 根据uid分组求和,并有having筛选大于10000的

select uid as 成员编号,sum(rmoney) as 金额 from tb_inoutinfo where rmoney>0 group by uid having sum(rmoney)>10000

--17、求入支出项目个数,即在项目表中按收支类型分组,再计数

select xid,sum(rmoney) from tb_inoutinfo group by xid

--联表查询

--18、在收支项目表和日常收支表中查询项目编号,项目名称,收支类型,收支金额

select rid,xname,xtype,rmoney from tb_inoutinfo a left join tb_inoutfield b on a.xid=b.xid

--19、在成员表和日常收支表中查询家庭角色,姓名,金额,操作日期

select a.uid,uname,upart,rmoney,rdate from tb_users a join tb_inoutinfo b on a.uid=b.uid

--20、在收支项目表和日期收支表,成员表中查询姓名,项目名称,收支金额

select uname,xname,xtype,rmoney,rdate from tb_users a join tb_inoutinfo b on a.uid=b.uid join tb_inoutfield c on b.xid=c.xid

本文来源:https://www.bwwdw.com/article/lkev.html

Top