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

问个sql解决办法

2012-09-03 
问个sql两个表,表头表和表体表。取表头表里的最大时间,max(cmaketime)的单据id通过这个id,取物料的表体的单

问个sql
两个表,表头表和表体表。

取表头表里的最大时间,max(cmaketime)的单据id

通过这个id,取物料的表体的单价,

两个表是通过id关联的,

问下sql怎么写,谢谢!

[解决办法]

SQL code
--> 测试数据:@Tdeclare @表头 table([id] int,[cmaketime] int)insert @表头select 1,6 union allselect 1,7 union allselect 1,8 union allselect 2,12 union allselect 2,13 union allselect 2,14 union allselect 3,25 union allselect 3,26 union allselect 3,28--> 测试数据:@表体declare @表体 table([id] int,[单价] int)insert @表体select 1,3 union allselect 2,5 union allselect 3,6select     *,(select max([cmaketime]) from @表头 where id=t.id) as [cmaketime] from @表体 t/*id          单价          cmaketime----------- ----------- -----------1           3           82           5           143           6           28*/ 

热点排行