我们的项目代码中有两条SQL查询语句:1. select *
from datainput t
where t.templateid =
(select max(templateid) from datainput d where t.unitid = d.unitid);
2. select * from datainput t where t.templateid = (select max(templateid) from datainput);其中第1条语句是项目中别人写的,我的理解是将datainput表中templateid最大的记录数显示出来(templateid有重复), 但是在我的看来where t.unitid = d.unitid就是多余的所以如果我写的话,我肯定会写成第2条SQL语句的形式, 请问大家的是这两条SQL语句有区别吗,我的理解是否正确? 是否第1条SQL语句的自连接查询效率会更高?谢谢!
from datainput t
where t.templateid =
(select max(templateid) from datainput d where t.unitid = d.unitid);
2. select * from datainput t where t.templateid = (select max(templateid) from datainput);其中第1条语句是项目中别人写的,我的理解是将datainput表中templateid最大的记录数显示出来(templateid有重复), 但是在我的看来where t.unitid = d.unitid就是多余的所以如果我写的话,我肯定会写成第2条SQL语句的形式, 请问大家的是这两条SQL语句有区别吗,我的理解是否正确? 是否第1条SQL语句的自连接查询效率会更高?谢谢!
解决方案 »
- 新手发问!!
- oracle不小心吧表truncate了,求帮助
- 在oracle中,怎么将一个number(integer)转换为'233.243.34.123'的字符串类型?
- 用 Oracle DBA Studio 启动服务出现"ORA-01078 处理系统参数失败" 是什么错误!
- 急!!!请问如何在脚本中获取数据库中某张表的字段类型!!!
- 检索有效的记录时间过长的解决办法?(现结)
- 在VC6中,如何和ORACLE数据库连接?
- delphi中调用过程如何开始客户数据库事务处理?急急急!
- Oracle的简单问题
- ArcSDE for Oracle11g安装问题
- 能不能实现类似ROWNUM 的功能?
- 求oracle里游标的使用,返回类型是临时表
templateid t.unitid
1 1
2 2
3 1
4 2
第一句的查询结果为
3 1
4 2
第二个则为
4 2
这个类似group by 分组..你可以执行一下两条PUBS库的数据看看就知道了..
1: 表示同一组内,最高的价格
select * from titles t where t.price = (select max(price) from titles d where t.pub_id = d.pub_id)
2: 表示不分类型,只要取最高的价格
select * from titles t where t.price = (select max(price) from titles d )语句写 好了效率就高...
第2条语句没有分组,只是求出全部记录中的templateid最大的记录
where t.templateid =
(select max(templateid) from datainput d where t.unitid = d.unitid);和select * from datainput t
where t.templateid in (select max(templateid) from datainput group by unitid);这两条SQL语句所查询出来的结果是相同的吗 ?
晕倒掉...那你换系统库..
select * from scott.emp e where e.sal = (select max(sal) from scott.emp m where e.deptno = m.deptno);
select * from scott.emp e where e.sal = (select max(sal) from scott.emp m);这两句执行的结果页是不一样的..
同一部门最高工资的.
跟同一个公司的最高工资..这样明白了吧...⊙﹏⊙b汗..
where t.templateid =
(select max(templateid) from datainput d where t.unitid = d.unitid); 和 select * from datainput t
where t.templateid in (select max(templateid) from datainput group by unitid); 这两条SQL语句所查询出来的结果是相同的吗 ?
结果肯定是不一样的..这条是多对多的
select * from datainput t where t.templateid = (select max(templateid) from datainput d where t.unitid = d.unitid);
这条是多对一的,跟你没写group by 的结果是一样的...
select * from datainput t where t.templateid in (select max(templateid) from datainput group by unitid);
第一个是只有当t.unitid = d.unitid时的最大值。
第二条是所有信息的最大值。
可以说完全是两个意思