3

我有两张桌子。

购买:

  • PurchaseId
  • SenderId
  • ReceiverId

顾客:

  • Customerid(主要-唯一)
  • CUSID(不是主要的-不是唯一的-允许为空)
  • FirsName
  • LastName

我想在客户表中使用用户名和姓氏对所有购买进行一些报告。我无法建立任何关系。我所有的代码都给我错误或返回错误的结果。请帮助我。(接收者和发送者的值在客户表中是相等的 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) 
4

2 回答 2

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

祝你好运。

于 2013-01-19T02:23:56.970 回答
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
于 2013-01-19T07:04:33.257 回答