如何从select * from中添加行号并获取所有列

时间:2019-05-09 07:32:02

标签: sql sql-server

我想从表的选择联接列中添加行号。我想把所有的栏目都取而不仅仅是某些栏目。我需要在下面修改一些代码,但我不知道如何。需要您的帮助

SELECT *
  FROM (SELECT ROW_NUMBER() OVER (ORDER BY SMSAuctionItemId DESC) AS row
              ,a.SMSAuctionItemId
              ,a.nopolisi
              ,b.MerkName
              ,c.SeriName
              ,d.SilinderName
              ,e.TipeName
              ,a.warna
              ,f.SubTipeName
          FROM SMS_AuctionItemView a
          LEFT JOIN MsMerk b ON a.MerkId=b.MerkId
          LEFT JOIN MsSeri c ON a.SeriId=c.SeriId
          LEFT JOIN MsSilinder d ON a.SilinderId=d.SilinderId
          LEFT JOIN MsTipe e ON a.TipeId=e.TipeId
          LEFT JOIN MsSubTipe f ON a.SubTipeId = f.SubTipeId 
         where CreateUserId= '8119'
           and a.CompanyId = 1
           and f.SubTipeId=15) x
 WHERE row > 0
   and row <= 15

如您在上方看到的,我要在下方删除

a.SMSAuctionItemId,a.nopolisi,b.MerkName, c.SeriName,d.SilinderName, e.TipeName, a.warna, f.SubTipeName

并检索所有列,但是当我删除它时,只给我行号列。

1 个答案:

答案 0 :(得分:0)

您可以通过选择方式选择其他表格中的所有列

SELECT ROW_NUMBER() OVER (ORDER BY SMSAuctionItemId DESC) AS row
              ,a.*

              ,b.*
              ,c.*
              ,d.*
              ,e.*

              ,f.*
          FROM SMS_AuctionItemView a
          LEFT JOIN MsMerk b ON a.MerkId=b.MerkId
          LEFT JOIN MsSeri c ON a.SeriId=c.SeriId
          LEFT JOIN MsSilinder d ON a.SilinderId=d.SilinderId
          LEFT JOIN MsTipe e ON a.TipeId=e.TipeId
          LEFT JOIN MsSubTipe f ON a.SubTipeId = f.SubTipeId 
         where CreateUserId= '8119'
           and a.CompanyId = 1
           and f.SubTipeId=15

但是当您将其作为子查询使用时,如果多个表的任何列名称均相同,则将为MerkId抛出错误,因为该列算法存在于{{1}中}这两个表。因此您必须为多个表中存在的相同列名使用别名