Hello,
I am trying to work out a solution I found by a good old google search but am getting nothing but unhelpful syntax errors.
Here is the link to the short answer that worked for the other person: SQL Join, 2 tables, same fields
In case you don’t want to read through that, here is the basic idea. Two tables. Table 1 has messages with IDs stored for the MsgFrom and MsgTo. Table 2 is those users (ID, username). It looks like you have to do two joins. This is how the solution reads.
select m.*, u1.userName as Sender, u2.userName as Recipient
from tabMessages as m
inner join tabUsers as u1
on u1.userId=m.msgFrom
inner join tabUsers as u2
on u2.userId=m.msgTo
where m.msgId = @someParameter;
My problem seems similar enough that this should get me what I need. Here is what I have specifically (trimmed down for simplicity).
Issues table:
[table=“width: 500, class: grid, align: left”]
[tr]
[td]issueid[/td]
[td]description[/td]
[td]addedby[/td]
[td]assignedto[/td]
[/tr]
[tr]
[td]1[/td]
[td]heat[/td]
[td]1[/td]
[td]2[/td]
[/tr]
[tr]
[td]2[/td]
[td]water[/td]
[td]1[/td]
[td]3[/td]
[/tr]
[tr]
[td]3[/td]
[td]roof[/td]
[td]2[/td]
[td]1[/td]
[/tr]
[/table]
Users table:
[table=“width: 500, class: grid, align: left”]
[tr]
[td]userid[/td]
[td]username[/td]
[/tr]
[tr]
[td]1[/td]
[td]Adam[/td]
[/tr]
[tr]
[td]2[/td]
[td]Brian[/td]
[/tr]
[tr]
[td]3[/td]
[td]Chad[/td]
[/tr]
[/table]
What I am looking for is a query that will output the following:
[table=“width: 500, class: grid, align: left”]
[tr]
[td]issueid[/td]
[td]description[/td]
[td]addedby[/td]
[td]assignedto[/td]
[/tr]
[tr]
[td]1[/td]
[td]heat[/td]
[td]Adam[/td]
[td]Brian[/td]
[/tr]
[tr]
[td]2[/td]
[td]water[/td]
[td]Adam[/td]
[td]Chad[/td]
[/tr]
[tr]
[td]3[/td]
[td]roof[/td]
[td]Brian[/td]
[td]Adam[/td]
[/tr]
[/table]
I think part of the problem may be that I am working in Microsoft Access. Here is the query I tried based on the solution above. I just get the same syntax error each time.
SELECT m.*, f.username AS AddedBy, t.username AS AssignedTo FROM issues AS m
INNER JOIN users AS f on m.addedby = f.userid
INNER JOIN users AS t on m.assignedto = t.userid;
Any idea what I am doing wrong? Thank you in advance for your help.
Swani