Assuming sender_id
and receiver_id
are references to a user_id
, and assuming that user_id
is UNIQUE in the Users
table... yes.
Join to the Users
table twice.
For example:
SELECT t.sender_id
, t.receiver_id
, t.value
, s.user_id AS sender_user_id
, s.username AS sender_username
, r.user_id AS receiver_user_id
, r.username AS receiver_username
FROM `Transactions` t
LEFT
JOIN `Users` s ON s.user_id = t.sender_id
LEFT
JOIN `Users` r ON r.user_id = t.receiver_id
ORDER BY 1,2,3
The query is using outer joins... the row from Transactions
will be returned if receiver_id
or sender_id
doesn't match a row in the Users
table.
The keyword LEFT
can be removed to change that behavior, so a row will be returned only if there is a matching row in Users
for both sender_id
and receiver_id
.