简体   繁体   中英

Getting 2 different records rows in the same sql join

I have 2 tables:

users

  • id_user
  • name
  • picture_url

matches

  • id_match
  • date_match
  • id_user_winner
  • id_user_loser

The query should bring me the list of matches where users.id_user=3 has been the winner. The catch is that the query should bring the name and picture_url from the winner and the loser at the same time.

The curreny query is:

SELECT u.name, u.picture_url, m.id_user_winner, m.id_user_loser, m.date_match FROM matches AS m
INNER JOIN users AS u ON u.id_user = m.id_user_winner WHERE u.id_user = 3

But that will only get me the name and picture of the winner. I need the name and picture of the loser as well. Any ideas? Thanks!

SELECT u.name
     , u.picture_url
     , m.id_user_winner
     , m.id_user_loser
     , e.name
     , e.picture_url
     , m.date_match 
FROM matches m JOIN users u ON u.id_user = m.id_user_winner AND u.id_user = 3
               JOIN users e ON e.id_user = m.id_user_loser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM