English 中文(简体)
从上一个最新记录中选取一列
原标题:SELECTING a column from a previous latest record

考虑下列 MySQL 表格:


应用程序(包含所有用户的所有应用程序)

unique_id |  user_id  |  date_of_application  |  date_ended  | score  |  status
--------------------------------------------------------------------------------
1            user_a          2010-09-09          2010-12-24     1.2      Ended
2            user_a          2011-03-03          2011-06-06     1.3      Ended
3            user_a          2011-08-08          2011-10-10     1.0      Ended

4            user_b          2010-09-09          2010-12-24     2.2      Ended
5            user_b          2011-03-03          2011-06-06     1.5      Ended

6            user_a          2012-01-01                                  Active
7            user_b          2012-01-02                                  Active
8            user_c          2012-01-03                                  Active
9            user_d          2012-01-04                                  Active


预期结果:

user_id  |  date_of_application  |  score  |  status
------------------------------------------------------
user_a          2011-01-01           1.0      Active
user_b          2011-01-02           1.5      Active

user_c          2011-01-03           10       Active
user_d          2011-01-04           10       Active


要解释; 我想选择/ 播放所有有 < 坚固 > 状态的记录 = 主动 < / 坚固 > 。 此外, 那些不是第一次申请的用户( 用户_a 和用户_b) 的得分将设定为“ 坚固” 先前的、最新的 < / 坚固” 的( 应用程序表格中的粗体部分) 。 < / 坚固] 另一方面, 首次申请的用户( 用户_ c 和用户_d) 的得分将设定为 10 。


说明/重申:

  • Assume that score for Ended applications/records will always be positive and not null
  • user_c and user_d are first time applicants
  • the applications table will have multiple records of the same users over time, BUT users can only have ONE Active application/record at a time


I have the following to start with; this(or a query similar to this) gave me either NULL or 0 values for the score column


SELECT userid_, date_of_application, status,
score = 
(
     SELECT score 
     FROM applications
     WHERE status =  Ended  AND 
           date_of_application = (SELECT MAX(date_of_application)
                                  FROM applications
                                  WHERE status= Ended )
)

FROM applications

WHERE 
status =  Active 

ORDER BY 
score ASC,
date_of_application ASC           


What am I missing here?
TIA.

最佳回答

Considering you want the score to be on the basis of latest. Try this -

SELECT apps.user_id, apps.date_of_application, apps.status,
       IFNULL(
              (SELECT app.score 
              FROM applications app
              WHERE  app.user_id = apps.user_id
              AND app.status =  Ended  
              ORDER BY app.date_ended DESC
              LIMIT 1), 10) AS score
FROM applications apps
WHERE  apps.status =  Active 
ORDER BY apps.score ASC,
         apps.date_of_application ASC 
问题回答

它应发挥作用:

SELECT user_id, date_of_application, status, coalesce(latest.score, 10) score
FROM applications
LEFT OUTER JOIN 
  (SELECT user_id, score 
   FROM applications a
   WHERE status =  Ended  AND 
         date_of_application = (SELECT MAX(date_of_application)
                                FROM applications
                                WHERE status= Ended  AND user_id = a.user_id)) latest 
                                  ON latest.user_id = applications.user_id
WHERE status =  Active 
ORDER BY date_of_application ASC




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签