我有两张桌子。
采购:
PurchaseId
SenderId
ReceiverId
客户:
Customerid
(小学唯一)CUSID
(非主要 - 非唯一 - 允许空)FirsName
LastName
我想通过customers表中的用户名和姓氏来购买所有报告。 我无法建立任何关系。我的所有代码都会给我错误或返回错误的结果。 请帮帮我。(接收者和发送者的值在客户表中等于CUSID) 关系是错误的:因为我不能创建任何主键,因为我的字段是允许为空。
(select (tbl_Customers.FirstName + '-' + tbl_Customers.LastName) as receiver
FROM tbl_PurchaseForms
INNER JOIN
tbl_Customers ON tbl_PurchaseForms.ReceiverUId = tbl_Customers.CUSID)
or
(select (tbl_Customers.FirstName + '-' + tbl_Customers.LastName) as sender
FROM tbl_PurchaseForms
INNER JOIN
tbl_Customers ON tbl_PurchaseForms.SenderUId = tbl_Customers.CUSID)
答案 0 :(得分:0)
我确实对你的问题感到有点困惑,但听起来你正试图归还所有收到或从你的购买表中发送东西的客户,链接你的cusid字段?
如果是这样的话,这样的事情应该有用(如果你需要区分发件人和接收者,我已经包含了一个名为购买类型的额外栏目):
SELECT
'Sender' as PurchaseType,
C.FirstName,
C.LastName
FROM Customers C
JOIN Purchase P ON C.CUSID = P.SenderId
UNION
SELECT
'Receiver' as PurchaseType,
C.FirstName,
C.LastName
FROM Customers C
JOIN Purchase P ON C.CUSID = P.ReceiverId
祝你好运。
答案 1 :(得分:0)
Select p.*
,(Select c.FirstName + '-' + c.LastName from customers c where c.CUSID = P.ReceiverId) as receiver
,(Select c.FirstName + '-' + c.LastName from customers c where c.CUSID = P.SenderId) as Sender
from purchase p
虽然购买应设计为启用此
Select p.*
,(Select c.FirstName + '-' + c.LastName from customers c where c.Customerid = P.ReceiverId) as receiver
,(Select c.FirstName + '-' + c.LastName from customers c where c.Customerid = P.SenderId) as Sender
from purchase p