user3787624
user3787624

Reputation: 3

How make double join in MySQL

I have two tables, user and transactions. In the transactions table, I would like to recover the pseudo associated with the id.

The table scheme :

transaction: id id_from id_to
user:        id pseudo

I would like to print :

ID PSEUDO_FROM PSEUDO_TO

Now, my sql query is:

SELECT *, t.id AS t_id
FROM transactions t LEFT JOIN user u ON t.t_from = u.id
WHERE (t.t_from=? OR t.t_to=?)
ORDER BY t.time DESC

But I have only pseudo_from.

Regards and thanks for help.

EDIT : I have adapt the query and it's work !

SELECT * , t.id AS t_id, u_from.pseudo AS from_pseudo, u_to.pseudo AS to_pseudo FROM transactions t LEFT JOIN user u_from ON t.t_from = u_from.id LEFT JOIN user u_to ON t.t_to = u_to.id (t.t_from=? OR t.t_to=?) ORDER BY t.time DESC

Upvotes: 0

Views: 43

Answers (1)

juergen d
juergen d

Reputation: 204756

SELECT u_from.pseudo as from_pseudo,
       u_to.pseudo as to_pseudo,
FROM transactions t 
JOIN user u_from ON t.id_from = u_from.id
JOIN user u_to ON t.id_to = u_to.id
WHERE ? IN (t.id_from, t.id_to)
ORDER BY t.time DESC

Upvotes: 1

Related Questions