简体   繁体   English

Mysql 加入查询从其他表中检索具有 ID 的用户的个人资料图像

[英]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此查询将仅获取两个图像,您需要添加额外的字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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