MySQL - Join 2 tables

前端 未结 1 976
执念已碎
执念已碎 2020-12-11 05:45

I have 2 tables: users & balance.

I want to join the tables with all of the details from the user table (all fields of all tuples) with the most recent entry from

1条回答
  •  感情败类
    2020-12-11 06:41

    You can use the first SQL you wrote but for all users:

    SELECT u.*, b.balance, b.date
    FROM users u JOIN balance b ON u.id = b.userId
    WHERE b.date = (SELECT MAX(date) FROM balance WHERE userId = u.id);
    

    This may not be the fastest way to get the result, but it'll give you what you need. I use similar queries in quite a few places in my app.

    0 讨论(0)
提交回复
热议问题