我需要编写一个select来生成以下内容:
r_country | s_country | s_name | r_name | msg | date
ca au bill bob 'hi' 2/12/2010
这是我的数据:
tableA
EmployeeName | EmployeeID | country
bill 1 au
bob 2 ca
tableB
Sender | Reciever | Msg | date
bill bob 'hi' 12/12/2010
之前我尝试过加入,但我认为这只会为我加入的用户返回数据
答案 0 :(得分:3)
你需要加入两次。
SELECT
receiver.country AS r_country,
sender.country AS s_country,
data.Sender AS s_name,
data.Receiver AS r_name,
data.msg AS msg,
data.date AS date
FROM
TableB AS data
LEFT JOIN
TableA AS sender
ON sender.EmployeeName = data.Sender
LEFT JOIN
TableA AS receiver
ON receiver.EmployeeName = data.Receiver
但请注意,您的数据(事实)表不应包含NAME。您应该使用实体的UniqueID,一个独立于实体属性的字段(例如名称,地址等)