如何在加入时为列编写查询

时间:2013-03-22 12:53:42

标签: sql-server-2008

select top 2  
    CONVERT(varchar(6),a.lastdate,6) as lastdate
    ,(select name from table_states where ID=a.joblocation ) as locat
    ,* 
from table_jobpost as a 
inner join (select * 
            from table_SuperAdmin as b 
            inner join table_Renewal as c on b.S_id=c.supid 
            where **c.proid** in (select [plan] 
                                    from table_features 
                                    where Fjobs=1) 
            and c.Payed=1) as aa on a.oid=aa.businessid 
where a.status=1 
and a.lastdate>GETDATE() 
order by startdate desc

在上面的查询c.proid中我想要:

select top 1 proid from table_Renewal  order by renewalid desc

如何重新排列查询?

现在在c.proid我有多个id.so它将检查所有ID。我想只检查

select top 1 proid from table_Renewal  order by renewalid desc

1 个答案:

答案 0 :(得分:0)

不确定我是否正确理解了您的问题。但请参阅下面的答案

从table_Renewal

中选择max(proid) 你可以试试吗

选择前2名     CONVERT(varchar(6),a.lastdate,6)作为lastdate     ,(从table_states中选择ID = a.joblocation的名称)作为locat     * 来自table_jobpost作为 内连接(选择*             从table_SuperAdmin作为b             内部联接table_Renewal as b on b.S_id = c.supid 左外连接(选择max(proid)as proid1,supid from table_Renewal group by supid)as c1 on b.S_id = c1.supid             其中c1.proid1 (选择[计划]                                     来自table_features                                     其中Fjobs = 1)             和c.Payed = 1)aa on a.oid = aa.businessid 其中a.status = 1 和a.lastdate> GETDATE() 按startdate desc命令