简体   繁体   中英

Mysql join query retrieving profile images of users with ids from other table

I'm having trouble with a join query, my issue is as follows.

Table: battles
Fields: id,attacker_id,defender_id

Table: users
Fields: id,profile_image

I would like to do a query to retrieve a battle and get the profile images as well from the other table.

Is there a way to do this in a single or do I have to do more than one?

Thanks in advance.

I wanted to wait a while to see if you had any attempt or if you will answer my first question to know if I understood the problem. But maybe you don't have a starting point. Try something like:

SELECT 
    a.profile_image as attacker_profile_image,
    d.profile_image as defender_profile_image
FROM 
    `battles` b
LEFT JOIN 
    `users` a 
ON 
    b.`attacker_id` = a.`id` 
LEFT JOIN 
    `users` d 
ON 
    b.`defender_id` = d.`id` 

the problem here is the fact that you need to join with the users table twice, so you will need to create aliases for the columns you plan to use

This query will fetch the two images only, you will need to add the extra fields

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