Sql server SQL Server按查询排序
我有两个名为Sql server SQL Server按查询排序,sql-server,stored-procedures,Sql Server,Stored Procedures,我有两个名为Listing和ListingProperties Listing (ID, CurrentPrice) ListingProperties (id, listingId, Fixedprice) 问题是我想按Fixedprice订购所有物品。但是有些物品没有固定价格 在这种情况下,我想检查当前价格并与其他价格进行比较Fixedprice,然后下单 上市 id name currentprice 1 a 10 2 b 50 3 c
Listing
和ListingProperties
Listing (ID, CurrentPrice)
ListingProperties (id, listingId, Fixedprice)
问题是我想按Fixedprice
订购所有物品。但是有些物品没有固定价格
在这种情况下,我想检查当前价格并与其他价格进行比较Fixedprice
,然后下单
上市
id name currentprice
1 a 10
2 b 50
3 c 40
列表属性
id listingId Fixedprice
1 1 20
2 3 30
下单后,所需的订单为
name
a
c
b
试试这个:
SELECT Name
FROM Listing l
LEFT JOIN ListingProperties lp ON l.id=lp.listingid
ORDER BY ISNULL(lp.FixedPrice, l.currentprice)
你的问题不清楚,但我试着猜 1) 您必须加入您的表:
SELECT <fields here>
FROM Listing L
LEFT JOIN ListingProperties LP
ON L.ID = LP.ListingId
当X为空时,则Y,否则X END
与ISNULL(X,Y)
相同(除了更长)
SELECT CASE WHEN L.CurrentPrice IS NULL THEN LP.FicedPrice ELSE L.CurrentPrice END
FROM Listing L
LEFT JOIN ListingProperties LP
ON L.ID = LP.ListingId
ORDER BY CASE WHEN L.CurrentPrice IS NULL THEN LP.FicedPrice ELSE L.CurrentPrice END