SQL在JOIN语句中JOIN两个表

时间:2015-04-10 09:13:07

标签: sql sql-server tsql

如何在此过程中将表“a”加入到其他表中?

我有两张桌子,想加入tbl_adstbl_state

    ALTER PROCEDURE [dbo].[GetAdPageWise]  
        @PageIndex INT = 1  
       ,@PageSize INT = 3  
       ,@PageCount INT OUTPUT  
    AS  
    BEGIN  
          SET NOCOUNT ON;  

        SELECT ROW_NUMBER() OVER  
                (  
                      ORDER BY [Id] DESC  
                )AS RowNumber  
          ,a.Id  
           ,a.ad_title
           ,a.ad_state
           ,a.ad_city
          ,a.ad_brief  
          ,a.ad_pic    
        INTO #Results  
        FROM [tbl_ads] a 


    END

2 个答案:

答案 0 :(得分:2)

这是导致错误的地方。尝试为您的行号添加别名,而不是[ID][a.Id]

 SELECT ROW_NUMBER() OVER  
                    (  
                          ORDER BY [a.Id] DESC  
                    )AS RowNumber  

此外,您可以使用其他答案在adesc

中使用ON关键字加入另一个表格

所以整个查询就像这样

SELECT ROW_NUMBER() OVER ( ORDER BY [a.Id] DESC )AS RowNumber ,
a.Id ,
a.ad_title ,
a.ad_state ,
a.ad_city ,
a.ad_brief ,
a.ad_pic 
INTO #Results 
FROM [tbl_ads] a 
join tbl_state b 
ON a.ad_city=b.Id

答案 1 :(得分:1)

您只需在查询中添加“JOIN tbl_state ON”即可。