Union all的用法实例sql

时间:2021-03-22 05:11:37

---Union all的用法实例sql
SELECT TOP (100) PERCENT ID, bid_user_id, UserName, amount, createtime, borrowType
FROM (SELECT bid.ID, bid.bid_user_id, reg.UserName, bid.amount, bid.createtime, '普通标' AS borrowType
FROM dbo.YYD_Borrow_BidRecord AS bid
INNER JOIN dbo.YYD_Users_RegInfo AS reg ON reg.ID = bid.bid_user_id
WHERE (bid.ID NOT IN(SELECT bid
FROM dbo.HUR_NewPlanLoan_ItemLoan)) AND (bid.ID NOT IN
(SELECT bidID FROM dbo.HUR_Product_Matching))

UNION ALL

SELECT bid.id, bid.userid, reg.UserName, bid.amount, bid.createtime, '计划' AS borrowType
FROM dbo.HUR_NewPlanLoan_BidRecord AS bid
INNER JOIN dbo.YYD_Users_RegInfo AS reg ON reg.ID = bid.userid

UNION ALL

SELECT buy.buyID, buy.userID, reg.UserName, buy.amount, buy.createDate, '随心买' AS borrowType
FROM dbo.HUR_Product_Buy AS buy
INNER JOIN dbo.YYD_Users_RegInfo AS reg ON reg.ID = buy.userID) AS data
ORDER BY bid_user_id, createtime DESC