向此结果集中添加(选择)另外两列

发布于 2024-12-11 06:23:51 字数 2336 浏览 0 评论 0原文

假设我有以下 MySQL 表:

user_id    date_of_application   date_ended    grade    status    
---------------------------------------------------------------

 1            2011-01-01         2011-02-28     1.0     Ended
 1            2011-02-02         2011-03-28     1.0     Ended
 1            2011-03-03         2011-04-28     (1.5)   Ended

 2            2011-01-01         2011-02-20     2.0     Ended
 2            2011-02-02         2011-03-11     2.5     Ended
 2            2011-03-03         2011-04-28     (1.0)   Ended

 1            2011-05-10              -          -      Pending
 2            2011-05-15              -          -      Pending

  • 请注意,表可以包含同一用户的多个记录,只要其之前的所有应用程序都已结束(状态 = 已结束)
  • user_id 不是唯一
  • 日期位于 yy-mm- dd 格式
  • date_endinggrade 仅在应用程序结束时更新

我想要在这里完成的是检索所有行(所有列)<强>状态在哪里“待定”,这样每个检索到的行的成绩列的值都是最新成绩的值(在上面的括号中),其中状态对于该特定用户(或行)来说“已结束”。

结果:

user_id    date_of_application   date_ended    grade    status    
---------------------------------------------------------------

 1            2011-05-10         2011-06-10     1.5      Pending
 2            2011-05-15         2011-06-15     1.0      Pending


----编辑-----:

我不确定是否需要为这些添加创建另一个线程('如果建议我这样做,就可以),无论如何--我决定采用 ypercube 的答案(见下文)(它有效,减去注释部分)。但是,除了上面的结果表之外,我还想再选择两列(每列来自不同的表):

给出所需表结果的工作代码(见上文):

SELECT user_id
     , date_of_application
     , date_ended
     , ( SELECT te.grade
         FROM TableX AS te
         WHERE te.status = 'Ended'
           AND te.user_id = t.user_id    

         ORDER BY te.date_ended DESC
         LIMIT 1
       ) AS grade
     , status
FROM TableX AS t
WHERE status = 'Pending'

现在我需要选择另外两列(is_first_time_username)以及前面提到的结果表。请注意,这两个表的 user_id 都是唯一的:

Table2:

user_id    is_firs_time_user
-----------------------------

 1               no
 2               no


表3:

user_id    name  
----------------------

 1        User A
 2        User B

Suppose I have the following MySQL table:

user_id    date_of_application   date_ended    grade    status    
---------------------------------------------------------------

 1            2011-01-01         2011-02-28     1.0     Ended
 1            2011-02-02         2011-03-28     1.0     Ended
 1            2011-03-03         2011-04-28     (1.5)   Ended

 2            2011-01-01         2011-02-20     2.0     Ended
 2            2011-02-02         2011-03-11     2.5     Ended
 2            2011-03-03         2011-04-28     (1.0)   Ended

 1            2011-05-10              -          -      Pending
 2            2011-05-15              -          -      Pending

  • note that the table can contain multiple records of the same user as long as all its previous applications have ended (status = ended)
  • user_id is not unique
  • date is in yy-mm-dd format
  • date_ended and grade are only updated the instant the application has ended

What I want to accomplish here is to retrieve all rows (together will all columns) WHERE status is 'Pending' and such that the value for the grade column for each of these retrieved rows is the value of the latest grade (in parenthesis above) where status is 'Ended' for this particular user (or row).

The result:

user_id    date_of_application   date_ended    grade    status    
---------------------------------------------------------------

 1            2011-05-10         2011-06-10     1.5      Pending
 2            2011-05-15         2011-06-15     1.0      Pending

----EDIT-----:

I'm not sure if I need to create another thread for these additions (' will do if I'm advised to do so), anyhow -- I decided to go with ypercube's answer (see below) (which works, minus the commented part). However, in addition to the above resulting table, I also would like to select two more columns (each from a different table):

Working code that gives-off the desired table result (see above):

SELECT user_id
     , date_of_application
     , date_ended
     , ( SELECT te.grade
         FROM TableX AS te
         WHERE te.status = 'Ended'
           AND te.user_id = t.user_id    

         ORDER BY te.date_ended DESC
         LIMIT 1
       ) AS grade
     , status
FROM TableX AS t
WHERE status = 'Pending'

Now I need to select these two other columns (is_first_time_user and name) together with the result table previously mentioned. Note that user_id for both of these tables are unique:

Table2:

user_id    is_firs_time_user
-----------------------------

 1               no
 2               no

Table3:

user_id    name  
----------------------

 1        User A
 2        User B

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尬尬 2024-12-18 06:23:51
SELECT user_d
     , date_of_application
     , date_ended
     , ( SELECT te.grade
         FROM TableX AS te
         WHERE te.status = 'Ended'
           AND te.user_id = t.user_id
           AND te.date_ended < t.date_of_application      --- not sure if 
                                                          --- this is needed
         ORDER BY te.date_ended DESC
         LIMIT 1
       ) AS grade
     , status
FROM TableX AS t
WHERE status = 'Pending'
SELECT user_d
     , date_of_application
     , date_ended
     , ( SELECT te.grade
         FROM TableX AS te
         WHERE te.status = 'Ended'
           AND te.user_id = t.user_id
           AND te.date_ended < t.date_of_application      --- not sure if 
                                                          --- this is needed
         ORDER BY te.date_ended DESC
         LIMIT 1
       ) AS grade
     , status
FROM TableX AS t
WHERE status = 'Pending'
月野兔 2024-12-18 06:23:51

这是一个包含派生内表 (ENDED) 的解决方案,该内表仅包含每个 user_id 的最新 Ended 记录。 (为了清楚起见,我省略了与获得成绩问题无关的列)。

 SELECT PEND.user_id, ENDED.grade FROM YourTable PEND
    INNER JOIN 
       (
         SELECT user_id, grade FROM YourTable T1
         WHERE status = 'Ended' AND NOT EXISTS 
            (SELECT * FROM YourTable T2 
             WHERE T2.user_id = T1.user_id AND T2.date_ended > T1.date_ended) 
       ) ENDED
       ON PEND.user_id = ENDED.user_id
    WHERE PEND.status = 'Pending'

如果 MySQL 在嵌套 SELECT 方面遇到问题,或者如果“每个用户最近结束的记录”的概念是您将在应用程序的其他地方使用的概念,或者如果您只是想更清楚,您可以这样做:

 CREATE VIEW MostRecentEndedGrade (user_id, grade) AS
    SELECT user_id, grade FROM YourTable T1
         WHERE status = 'Ended' AND NOT EXISTS 
            (SELECT * FROM YourTable T2 
             WHERE T2.user_id = T1.user_id AND T2.date_ended > T1.date_ended) 

 SELECT PEND.user_id, ENDED.grade 
    FROM YourTable PEND INNER JOIN MostRecentEndedGrade ENDED
    ON PEND.user_id = ENDED.user_id
    WHERE PEND.status = 'Pending'

Here is a solution that includes a derived inner table (ENDED) that contains only the most recent Ended record per user_id. (For clarity, I left out the columns that don't pertain to the question of getting the grade).

 SELECT PEND.user_id, ENDED.grade FROM YourTable PEND
    INNER JOIN 
       (
         SELECT user_id, grade FROM YourTable T1
         WHERE status = 'Ended' AND NOT EXISTS 
            (SELECT * FROM YourTable T2 
             WHERE T2.user_id = T1.user_id AND T2.date_ended > T1.date_ended) 
       ) ENDED
       ON PEND.user_id = ENDED.user_id
    WHERE PEND.status = 'Pending'

If MySQL has trouble with the nested SELECT, or if the concept of "most recent ended record per user" is one that you will use elsewhere in your application, or if you simply want more clarity, you can do this:

 CREATE VIEW MostRecentEndedGrade (user_id, grade) AS
    SELECT user_id, grade FROM YourTable T1
         WHERE status = 'Ended' AND NOT EXISTS 
            (SELECT * FROM YourTable T2 
             WHERE T2.user_id = T1.user_id AND T2.date_ended > T1.date_ended) 

 SELECT PEND.user_id, ENDED.grade 
    FROM YourTable PEND INNER JOIN MostRecentEndedGrade ENDED
    ON PEND.user_id = ENDED.user_id
    WHERE PEND.status = 'Pending'
傻比既视感 2024-12-18 06:23:51

我相信最简单的方法是使用此查询构建一个视图:

SELECT user_id, MAX(date_ended) AS max_date FROM table 
WHERE status='ENDED' 
GROUP BY user_id

然后执行此查询以获取每个用户的最后成绩:

SELECT t1.user_id, t1.date_of_application,
   (SELECT t.date_ended, t.grade FROM table t, view v 
   WHERE t.user_id=v.user_id 
   AND t.date_ended = v.max_date),
   t1.status FROM table t1
WHERE status='PENDING'

I believe the easiest way is to build a view with this query :

SELECT user_id, MAX(date_ended) AS max_date FROM table 
WHERE status='ENDED' 
GROUP BY user_id

then do this query to get the last grade for each user :

SELECT t1.user_id, t1.date_of_application,
   (SELECT t.date_ended, t.grade FROM table t, view v 
   WHERE t.user_id=v.user_id 
   AND t.date_ended = v.max_date),
   t1.status FROM table t1
WHERE status='PENDING'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文