MySQL社区

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 5977|回复: 1
打印 上一主题 下一主题

[SQL类] 请教sql语句嵌套

[复制链接]
跳转到指定楼层
1#
发表于 2007-8-29 16:07:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有一个公式,由下列4条sql语句计算得成:
--select count(ExamID) from MPPS where MatchTime >'2007-08-22 11:00:00.0' and MatchTime <'2007-08-23 11:00:00.0';
-- 13213
--select max(ExamID) from MPPS where MatchTime >'2007-08-22 11:00:00.0' and MatchTime <'2007-08-23 11:00:00.0';
-- 2155457
--select min(ExamID) from MPPS where MatchTime >'2007-08-22 11:00:00.0' and MatchTime <'2007-08-23 11:00:00.0';
-- 2133303
--select count(ExamID) from ExamInfo where ExamID > 2133303 and ExamID < 2155457;
-- 22153
auto match rate = 13213 / 22153 * 100% = 59.64%

请问如何将此4条sql语句嵌套成一条sql语句,且不需要输3遍日期?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 分享淘帖 顶 踩
2#
发表于 2007-8-29 16:12:02 | 只看该作者
当然可以,改写成:
mysql>
select @starttime:='2007-08-22 11:00';
select @endtime:='2007-08-23 11:00';
select @i_count:=count(ExamID) from MPPS where MatchTime >@starttime and MatchTime <@endtime;
select @i_max:=max(ExamID) from MPPS where MatchTime >@starttime and MatchTime <@endtime;
select @i_min:=min(ExamID) from MPPS where MatchTime >@starttime and MatchTime <@endtime;
select @ii_count:=count(ExamID) from ExamInfo where ExamID > @i_min and ExamID < @i_max;
select CONCAT(@i_count/@ii_count*100,'%') as "auto match rate" ;


一气呵成,直接执行,得出答案。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|申请友链|小黑屋|Archiver|手机版|MySQL社区 ( 京ICP备07012489号   
联系人:周生; 联系电话:13911732319

GMT+8, 2024-5-3 15:24 , Processed in 0.062977 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表