123
返回列表 发新帖
楼主: courset

[精华] 请高手指教一个SQL语句

[复制链接]
论坛徽章:
22
授权会员
日期:2005-10-30 17:05:332012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:36马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:11:36
21#
发表于 2004-1-28 21:58 | 只看该作者
最初由 magicangel 发布
[B]这样也可以:
select id,class,max(score),max(exam_date)
from exam_score
group by id,class
order by id

guanglin4的做法对于重复纪录要加distinct [/B]


本来我是想在我的基础上改成
select distinct a.* from exam_score a, (select id,class,max(exam_date) exam_date from exam_score group by id, class) b
where a.id=b.id and a.class=b.class and a.exam_date=b.exam_date
的,不过最近受了你的启发,我就用起exists来了。

使用道具 举报

回复
论坛徽章:
22
授权会员
日期:2005-10-30 17:05:332012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:36马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:11:36
22#
发表于 2004-1-28 22:17 | 只看该作者

使用道具 举报

回复
论坛徽章:
22
授权会员
日期:2005-10-30 17:05:332012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:36马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:11:36
23#
发表于 2004-1-28 22:23 | 只看该作者
test

使用道具 举报

回复
论坛徽章:
0
24#
发表于 2004-1-28 22:56 | 只看该作者
很有收获。

Here are some thoughts:

1) use of distinct
for this particular case, distinct may not be necessary.  However, if we generalize the case, then the issue becomes how to get a row or rows which is max or min in certain fields. In other words, dups are allowed in the table. In that case, the use of distinct will be determined by business requirements.

2) exists or not
The test table is very small, and has no indices.  In fact, even if indices were created, they probably would not be used any way, because the cost of a table scan will be much less in that case (to the optimizer).  For bigger tables (say, over 1 million of records), things will be different.  Not exists is not ideal in that case (definitely inferior to just "exists". Max/Min will probably not fare much better either.  We will only know it through tests and query plan analysis.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表