我希望使用主键 ClientID 从名为客户端的表中检索两个不同客户端的名称为“c”。
[销售详情] ,因为'sd'包含 clientID ,firstName,lastName,saleDate,saleAmount, recipientID 。 sd.clientID和sd.recipientID都是c.ClientID的外键。
以下是我目前使用的查询:
SELECT
sd.saleId AS 'Sale_ID'
,sd.clientId AS 'Client_ID'
,NULL
,c1.firstName + ' ' + c1.lastName AS 'Purchased_by'
,sd.description AS 'Purchased_pricing_option'
,sd.paymentAmount AS 'For_amount'
,CASE
WHEN sd.recipientID IS NULL THEN 'SELF'
ELSE sd.recipientID
END AS 'RecID'
,c2.firstName + ' ' + c2.lastName AS 'Purchased_for'
,sd.createdDateTimeUTC AS 'Purchased_on'
,NULL
,sd.grossUnitPrice AS 'Total_paid'
FROM [Sales Details] sd
INNER JOIN Clients c1
ON sd.clientID = c1.clientID
INNER JOIN Clients c2
ON sd.recipientId = c2.clientID
WHERE sd.SaleID = 'SALEIDHERE'
简而言之,我需要查看“购买者姓名”以及“收件人姓名”。我在思考正确的方向吗?
经过测试,这似乎完全正常。这种类型的设置会出现性能问题吗?不确定我理解你的建议,失败者。
INNER JOIN Clients c1
ON sd.clientID = c1.clientID
INNER JOIN Clients c2
ON sd.recipientId = c2.clientID