How to join two tables together with same number of rows by their order

前端 未结 9 1968
庸人自扰
庸人自扰 2020-12-19 04:38

I am using SQL2000 and I would like to join two table together based on their positions

For example consider the following 2 tables:

table1
-------
name
-         


        
9条回答
  •  伪装坚强ぢ
    2020-12-19 05:08

    would be best to use row_number(), but that is only for 2005 and 2008, this should work for 2000...

    Try this:

    create table table1 (name varchar(30))
    insert into table1 (name) values ('cat')
    insert into table1 (name) values ('dog')
    insert into table1 (name) values ('mouse')
    
    create table table2 (cost int)
    insert into table2 (cost) values (23)
    insert into table2 (cost) values (13)
    insert into table2 (cost) values (25)
    
    Select IDENTITY(int,1,1) AS RowNumber
    , Name
    INTO #Temp1
    from table1
    
    
    Select IDENTITY(int,1,1) AS RowNumber
    , Cost
    INTO #Temp2
    from table2
    
    select * from #Temp1
    select * from #Temp2
    
    SELECT
        t1.Name, t2.Cost
        FROM #Temp1                 t1
            LEFT OUTER JOIN #Temp2  t2 ON t1.RowNumber=t2.RowNumber
        ORDER BY t1.RowNumber
    

提交回复
热议问题