简体   繁体   English

mysql LEFT JOIN主表和另外两个表,每个表都有最大值

[英]mysql LEFT JOIN main table and two additional tables with max value from each

So i have three tables: 所以我有三个表:

Users
+-------+-----+----+
| id    | val1|val2|
+-------+-----+----+
| 1     |  1  |3   |
| 2     |  2  |5   |
| 3     |  4  |7   |
+-------+-----+----+

UsersData
+----+--------------+------------+-----|
| id | users_id     | created_at | gold|
+----+--------------+------------+-----|
|  9 |  1           |121454561212| 14  |
|  10|  1           |131454561212|  2  |
|  11|  2           |111454561212| 99  |
+----+--------------+------------+-----+

Extra
+----+------------+-----|
| id | users_id   | val4|
+----+------------+-----|
|  1 |  1         |  5  |
|  2 |  1         |  6  |
|  3 |  1         |  7  |
+----+------------+-----+

So what i wish to achieve(in a single query) is to get a single row result for user with id = 1, that holds: 所以我希望实现(在单个查询中)是为id = 1的用户获取单行结果,该结果满足:

  • everything from Users Table 用户表中的所有内容
  • gold value of the most recent entry for that user (users_id = 1, created_at = MAX) 该用户的最新条目的黄金值(users_id = 1,created_at = MAX)
  • biggest val4 from the Extra table, where users_id = 1 Extra表中最大的val4,其中users_id = 1

So the result row would look like this: 因此结果行将如下所示:

+-------+-----+----+-----+----+
| id    | val1|val2|gold |val4|
+-------+-----+----+-----+----|
| 1     |  1  |3   | 2   |  7 |
------------------------------+

I can get The first part done with 我可以完成第一部分

SELECT Users.id, Users.val1, Users.val2, UsersData.gold
FROM UsersData
LEFT JOIN Users ON UsersData.users_id = Users.id
WHERE Users.id = 1 
ORDER BY UsersData.created_at DESC
LIMIT 1

and the second part with 第二部分

SELECT MAX(Distances.distance) AS maxdistance FROM Distances WHERE Distances.users_id = 1

But i can't combine them no matter how i try... I would really like to have this done in single query, obviously i can do it with multiple - but i believe it is just my lack of mysql skills that is the issue here. 但是无论如何尝试,我都无法将它们组合在一起……我真的很想在单个查询中完成此任务,显然我可以使用多个查询来完成-但我认为这只是我缺乏mysql技能而已这里。

Thanks! 谢谢!

Just use subquery: 只需使用子查询:

SELECT Users.id, Users.val1, Users.val2, UsersData.gold,
(SELECT MAX(Distances.distance) FROM Distances WHERE Distances.users_id = Users.id) AS maxdistance
FROM UsersData
RIGHT JOIN Users ON UsersData.users_id = Users.id
WHERE Users.id = 1 
ORDER BY UsersData.created_at DESC
LIMIT 1

This is subquery connected by Users.id: 这是由Users.id连接的子查询:

SELECT MAX(Distances.distance) FROM Distances WHERE Distances.users_id = Users.id) AS maxdistance

I would use subqueries like this: 我将使用以下子查询:

select u.*,
       (select ud.gold
        from userdata ud
        where ud.users_id = u.id
        order by ud.created_at desc
        limit 1
       ) as most_recent_gold,
       (select max(e.val4)
        from extra e
        where e.users_id = u.id
       ) as max_val4
from users u
where u.id = 1 ;

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

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