<bdo id='ej1Ky'></bdo><ul id='ej1Ky'></ul>

    1. <small id='ej1Ky'></small><noframes id='ej1Ky'>

      <legend id='ej1Ky'><style id='ej1Ky'><dir id='ej1Ky'><q id='ej1Ky'></q></dir></style></legend>
    2. <i id='ej1Ky'><tr id='ej1Ky'><dt id='ej1Ky'><q id='ej1Ky'><span id='ej1Ky'><b id='ej1Ky'><form id='ej1Ky'><ins id='ej1Ky'></ins><ul id='ej1Ky'></ul><sub id='ej1Ky'></sub></form><legend id='ej1Ky'></legend><bdo id='ej1Ky'><pre id='ej1Ky'><center id='ej1Ky'></center></pre></bdo></b><th id='ej1Ky'></th></span></q></dt></tr></i><div id='ej1Ky'><tfoot id='ej1Ky'></tfoot><dl id='ej1Ky'><fieldset id='ej1Ky'></fieldset></dl></div>

    3. <tfoot id='ej1Ky'></tfoot>
    4. MySQL - 加入 2 个表

      时间:2023-10-09
        <tbody id='Jr7Zu'></tbody>

      1. <i id='Jr7Zu'><tr id='Jr7Zu'><dt id='Jr7Zu'><q id='Jr7Zu'><span id='Jr7Zu'><b id='Jr7Zu'><form id='Jr7Zu'><ins id='Jr7Zu'></ins><ul id='Jr7Zu'></ul><sub id='Jr7Zu'></sub></form><legend id='Jr7Zu'></legend><bdo id='Jr7Zu'><pre id='Jr7Zu'><center id='Jr7Zu'></center></pre></bdo></b><th id='Jr7Zu'></th></span></q></dt></tr></i><div id='Jr7Zu'><tfoot id='Jr7Zu'></tfoot><dl id='Jr7Zu'><fieldset id='Jr7Zu'></fieldset></dl></div>
        • <bdo id='Jr7Zu'></bdo><ul id='Jr7Zu'></ul>

            <small id='Jr7Zu'></small><noframes id='Jr7Zu'>

              <legend id='Jr7Zu'><style id='Jr7Zu'><dir id='Jr7Zu'><q id='Jr7Zu'></q></dir></style></legend>

            • <tfoot id='Jr7Zu'></tfoot>
              • 本文介绍了MySQL - 加入 2 个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有 2 个表:users &平衡.

                I have 2 tables: users & balance.

                我想将包含来自用户表(所有元组的所有字段)的所有详细信息的表与余额表中的最新条目(由用户 ID 链接的 1 个字段)连接起来.

                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 the balance table (1 field linked by a user id).

                表的结构如下:

                余额:

                +---------+
                | Field   |
                +---------+
                | dbid    |
                | userId  |
                | date    |
                | balance |
                +---------+
                

                用户:

                +-------------+
                | Field       |
                +-------------+
                | dbid        |
                | id          |
                | fName       |
                | sName       |
                | schedName   |
                | flexiLeave  |
                | clockStatus |
                +-------------+
                

                我已经尝试了几个小时来做到这一点,我能得到的最接近的是为单个用户返回一行:

                I have been trying for hours to do this and the closest I can get is to return a row for a single user:

                SELECT u.*, b.balance, b.date 
                FROM users u, balance b 
                WHERE
                    u.id = b.userId AND
                    b.date = (SELECT MAX(date) FROM balance WHERE userId = 'A8126982');
                

                或者我可以选择所有用户但不选择余额表中的最新条目:

                Or I can select all users but not the most recent entry in the balance table:

                SELECT u.*, b.balance, b.date 
                FROM users u, balance b 
                WHERE u.id = b.userId GROUP BY u.id;
                

                我尝试了许多不同的查询,似乎越来越接近,但我就是无法到达我想去的地方.

                I have tried many different queries and seem to be getting closer but I just can't get to where I want to be.

                任何帮助将不胜感激.

                推荐答案

                您可以使用您编写的第一个 SQL,但适用于所有用户:

                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.

                这篇关于MySQL - 加入 2 个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Oracle - 更新连接 - 非键保留表 下一篇:根据条件连接两个表以及第二个表中的记录数

                相关文章

              • <i id='QIJ9R'><tr id='QIJ9R'><dt id='QIJ9R'><q id='QIJ9R'><span id='QIJ9R'><b id='QIJ9R'><form id='QIJ9R'><ins id='QIJ9R'></ins><ul id='QIJ9R'></ul><sub id='QIJ9R'></sub></form><legend id='QIJ9R'></legend><bdo id='QIJ9R'><pre id='QIJ9R'><center id='QIJ9R'></center></pre></bdo></b><th id='QIJ9R'></th></span></q></dt></tr></i><div id='QIJ9R'><tfoot id='QIJ9R'></tfoot><dl id='QIJ9R'><fieldset id='QIJ9R'></fieldset></dl></div>

                  <small id='QIJ9R'></small><noframes id='QIJ9R'>

                1. <tfoot id='QIJ9R'></tfoot>

                  • <bdo id='QIJ9R'></bdo><ul id='QIJ9R'></ul>

                  1. <legend id='QIJ9R'><style id='QIJ9R'><dir id='QIJ9R'><q id='QIJ9R'></q></dir></style></legend>