首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

MSSQL 有没有last函数解决办法

2012-10-10 
MSSQL 有没有last函数就是主表,子表里有多条数,想拿最后一条[解决办法]没有这个函数,需要通过关系型查询完

MSSQL 有没有last函数
就是主表,子表里有多条数,想拿最后一条

[解决办法]
没有这个函数,
 需要通过关系型查询完成


SQL code
select * from 主表,子表 where 主表.id=子表的idand 子表的时间=(select max(时间字段) from 子表 as t /*这个 as t 不能省略*/where 子表的id=t.子表的id)
[解决办法]
有.

参考:
LAST_VALUE (Transact-SQL)
返回 SQL Server 2012 中有序值集中的最后一个值。

地址:
http://technet.microsoft.com/zh-cn/hh231517(v=sql.100)
[解决办法]
没有这个函数,用游标可以取出来


有排序字段直接

select top 1 * from tb order by 排序字段 desc
[解决办法]
SQL code
declare @T table([id] int,[col] varchar(1))insert @Tselect 1,'a' union allselect 2,'b' union allselect 3,'c'declare @C table([id] int,[tid] int,[col] int)insert @Cselect 1,1,23 union allselect 2,1,14 union allselect 3,1,18 union allselect 4,2,12 union allselect 5,2,14 union allselect 6,2,15 union allselect 7,3,12 union allselect 8,3,12 union allselect 9,3,10SELECT  a.* ,b.colFROM    @T a        LEFT JOIN @C b ON a.id = b.tidWHERE   b.id = ( SELECT MAX(id) FROM @C WHERE  tid = b.tid)/*id          col  col----------- ---- -----------1           a    182           b    153           c    10*/
[解决办法]
LAST(column)
返回在指定的域中最后一个记录的值(SQLServer2000 不支持)

热点排行