<bdo id='DIEvW'></bdo><ul id='DIEvW'></ul>
  • <legend id='DIEvW'><style id='DIEvW'><dir id='DIEvW'><q id='DIEvW'></q></dir></style></legend>

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

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

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

        MySQL INNER JOIN 从第二个表中只选择一行

        MySQL INNER JOIN select only one row from second table(MySQL INNER JOIN 从第二个表中只选择一行)

              <tbody id='2JSo8'></tbody>
            <legend id='2JSo8'><style id='2JSo8'><dir id='2JSo8'><q id='2JSo8'></q></dir></style></legend>
          • <small id='2JSo8'></small><noframes id='2JSo8'>

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

            • <tfoot id='2JSo8'></tfoot>

                  <bdo id='2JSo8'></bdo><ul id='2JSo8'></ul>
                  本文介绍了MySQL INNER JOIN 从第二个表中只选择一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 users 表和一个 payments 表,对于每个有付款的用户,在 payments表.我想选择所有有付款的用户,但只选择他们最近的付款.我正在尝试这个 SQL,但我以前从未尝试过嵌套 SQL 语句,所以我想知道我做错了什么.感谢帮助

                  I have a users table and a payments table, for each user, those of which have payments, may have multiple associated payments in the payments table. I would like to select all users who have payments, but only select their latest payment. I'm trying this SQL but i've never tried nested SQL statements before so I want to know what i'm doing wrong. Appreciate the help

                  SELECT u.* 
                  FROM users AS u
                      INNER JOIN (
                          SELECT p.*
                          FROM payments AS p
                          ORDER BY date DESC
                          LIMIT 1
                      )
                      ON p.user_id = u.id
                  WHERE u.package = 1
                  

                  推荐答案

                  您需要有一个子查询来获取每个用户 ID 的最新日期.

                  You need to have a subquery to get their latest date per user ID.

                  SELECT  a.*, c.*
                  FROM users a 
                      INNER JOIN payments c
                          ON a.id = c.user_ID
                      INNER JOIN
                      (
                          SELECT user_ID, MAX(date) maxDate
                          FROM payments
                          GROUP BY user_ID
                      ) b ON c.user_ID = b.user_ID AND
                              c.date = b.maxDate
                  WHERE a.package = 1
                  

                  这篇关于MySQL INNER JOIN 从第二个表中只选择一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to delete duplicate records in mysql database?(如何删除mysql数据库中的重复记录?)
                  Python Pandas write to sql with NaN values(Python Pandas 使用 NaN 值写入 sql)
                  MySQL Insert amp; Joins(MySQL 插入 amp;加入)
                  MySQL concat() to create column names to be used in a query?(MySQL concat() 创建要在查询中使用的列名?)
                  NodeJS responded MySQL timezone is different when I fetch directly from MySQL(当我直接从 MySQL 获取时,NodeJS 响应 MySQL 时区不同)
                  WHERE datetime older than some time (eg. 15 minutes)(WHERE 日期时间早于某个时间(例如 15 分钟))
                  <tfoot id='77Prp'></tfoot>
                        <bdo id='77Prp'></bdo><ul id='77Prp'></ul>
                      • <i id='77Prp'><tr id='77Prp'><dt id='77Prp'><q id='77Prp'><span id='77Prp'><b id='77Prp'><form id='77Prp'><ins id='77Prp'></ins><ul id='77Prp'></ul><sub id='77Prp'></sub></form><legend id='77Prp'></legend><bdo id='77Prp'><pre id='77Prp'><center id='77Prp'></center></pre></bdo></b><th id='77Prp'></th></span></q></dt></tr></i><div id='77Prp'><tfoot id='77Prp'></tfoot><dl id='77Prp'><fieldset id='77Prp'></fieldset></dl></div>

                          <legend id='77Prp'><style id='77Prp'><dir id='77Prp'><q id='77Prp'></q></dir></style></legend>
                              <tbody id='77Prp'></tbody>

                            <small id='77Prp'></small><noframes id='77Prp'>