SQL的新手,我想知道它是否适用于放置"或"选择列时的条件(SELECT)?
示例:
SELECT (Column1 OR Column2)
我的目标: 我目前正在创建一个视图,该视图将显示我将用于查询的所有声明类型基本信息。显然,它们位于不同的表中,因此我希望将它们合并到一个视图中以便于访问。问题是在创建时,视图将带有OR语句的列放到1或0,而它应该是VARCHAR(字符串)。
完整SQL查询:
CREATE VIEW ViewAllClaims AS
SELECT Claim.ClaimID, Claim.CreatedDate, (ReturnClaim.ClaimStatus or RepairClaim.ClaimStatus) as 'ClaimStatus', (SupplierName or FirstName) as 'Name'
FROM Claim, ReturnClaim, RepairClaim, Supplier, Product, ReturnClaimProduct, Customer
WHERE ( Claim.ClaimID = ReturnClaim.ClaimID
AND ReturnClaim.ClaimID = ReturnClaimProduct.ClaimID
AND ReturnClaimProduct.Keycode = Product.Keycode
AND Product.SupplierID = Supplier.SupplierID)
or
(Claim.ClaimID = RepairClaim.ClaimID
AND RepairClaim.CustomerID = Customer.CustomerID);
答案 0 :(得分:1)
您可以使用CASE
:
SELECT (
CASE
WHEN ReturnClaim.ClaimStatus IS NULL THEN RepairClaim.ClaimStatus
ELSE ReturnClaim.ClaimStatus
END
) AS 'ClaimStatus'
FROM ...