找回密码
 注册
搜索
热搜: 回贴
微赢网络技术论坛 门户 数据库 查看内容

SQL应用--查询最大版本值

2009-12-14 18:51| 发布者: admin| 查看: 27| 评论: 0|原作者: 九天玄女

★EmsNoCopGNoExgVersio......


EmsNo CopGNo ExgVersion
E57705000004 DSHC0741 60
E57705000003 DSHC0741 50
E57705000002 DSHC0741 36
E57705000001 DSHC0742 65
E57705000000 DSHC0742 75
需要的结果:
EmsNo CopGNo ExgVersion

E57705000004 DSHC0741 60
E57705000000 DSHC0742 75
既 CopGNO中每个产品的最大ExgVersion给取出来。
SELECT T.EMSNO,E.COPGNO,E.EXGVERSION ,T.begindate,t.enddate
FROM emsexgversion T ,(SELECT copgno,MAX(ExgVersion) as exgversion from emsexgversion group by copgno) E
WHERE T.COPGNO=E.COPGNO AND T.EXGVERSION=E.EXGVERSION
ORDER BY E.COPGNO
select a.* from emsexgversion a
where not exists(select 1 from emsexgversion where CopGNo=a.CopGNo and ExgVersion>a.ExgVersion)
select 1 from ...
通常存在于exists中.
也可以写做
select 2 from ...
或者任何常数.
因为exists只是判断为不为null来返回true或者false
通常的写法还有
select count(1) from ...
其实等同于
select count(*) from ...
或者
select count(主键) from ...
这样的SQL
根据主键及索引的建立方式,速度上有细小的差别.
而-1 = -1这样的表达式是永远返回true的.
就像 1 = 2是永远返回false一样.
我们通常copy表结构就经常用
create table newtable as
select * from oldtable where 1 = 2;
-1 = -1一般是写在条件不确定的用字符串拼出来的动态SQL中.
比方说:
我的where后面可能有5个条件,也可能一个也没有.
这时我会这样写
".... where 1 = 1"+str1+str2+...; -- 其中的str1,str2等有可能是空字符串.

最新评论

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 07:30 , Processed in 0.141038 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

返回顶部