各位大哥能帮帮忙看看这样的sql怎么写
表一 m_compens :  员工编号       契别            薪资                    0005          正式员           ?
                    0012          试用员           ?表二m_trndep :    员工编号        项             标准薪资
                    0005            1               700
                    0005            2               850
                    0005            3               1050
                    0012            1               600
现需要update  m_compens中的薪资=m_trndep中项(为最大)那项的标准薪资WHERE m_compens.员工编号=
m_trndep.员工编号 (其实其中正式员是取项次最大的那项,试用员只有一项取最大项也没有关系)

解决方案 »

  1.   

    update  m_compens set m_compens.薪资=(select Max(m_trndep.标准薪资) from m_trndep where m_compens.员工编号=m_trndep.员工编号)
      

  2.   

    update  m_compens as A set a.薪资=(select Max(b.标准薪资) from m_trndep as b where a.员工编号=b.员工编号)
      

  3.   

    update  m_compens set m_compens.薪资=(select top 1 标准薪资 from m_trndep where 员工编号=m_compens.员工编号 order by 项 desc)
      

  4.   

    我写的,手边没有调试工具,你自己试一试
    update a set a.新资='''+edit1.text+''' from m_compens a,m_trndep b where a.员工编号=b.员工编号 and b.标准薪资=(select max(标准薪资) from m_compens c,m_trndep d where a.员工编号=b.员工编号  )
      

  5.   

    declare @m_compens table (员工编号 char(4),契别 char(10),薪资 numeric(18,2))
    insert @m_compens values('0005',    '正式员'  ,     0.00)
    insert @m_compens values('0012' ,   '试用员'   ,    0.00)declare @m_trndep table (员工编号 char(4),项 char(2),标准薪资 numeric(18,2))
    insert @m_trndep values('0005'  ,  '1' ,  700 )
    insert @m_trndep values('0005'  ,  '2' ,  850 )
    insert @m_trndep values('0005'  ,  '3' ,  1050 )
    insert @m_trndep values('0012'  ,  '1' ,  600 )
    --以下是你要的update语句
    update @m_compens 
    set 薪资= tab.xz
    from 
    (select b.标准薪资 as xz,b.员工编号 as bh from @m_compens a,@m_trndep b 
    where b.员工编号=a.员工编号 
    and 项 = (select max(项) from @m_trndep where 员工编号=a.员工编号)) as tabwhere 员工编号 = bh--以下查询结果
    select * from @m_compens
      

  6.   

    还是这样简单些:
    update m_compens 
         set薪资 = (select 标准薪资 from m_trndep where 员工编号 = m_compens.员工编号 
        and 项 = (select max(项) from m_trndep where 员工编号=m_compens.员工编号))
      

  7.   

    update  m_compens set m_compens.薪资=(select m_trndep.标准薪资
    from m_trndep  where m_compens.员工编号=m_trndep.员工编号 and m_trndep.项=MAX(m_trndep.项) )