向此结果集中添加(选择)另外两列
假设我有以下 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_ending 和 grade 仅在应用程序结束时更新
我想要在这里完成的是检索所有行(所有列)<强>状态在哪里“待定”,这样每个检索到的行的成绩列的值都是最新成绩的值(在上面的括号中),其中状态对于该特定用户(或行)来说“已结束”。
结果:
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_user 和 name)以及前面提到的结果表。请注意,这两个表的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个包含派生内表 (ENDED) 的解决方案,该内表仅包含每个 user_id 的最新 Ended 记录。 (为了清楚起见,我省略了与获得成绩问题无关的列)。
如果 MySQL 在嵌套 SELECT 方面遇到问题,或者如果“每个用户最近结束的记录”的概念是您将在应用程序的其他地方使用的概念,或者如果您只是想更清楚,您可以这样做:
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).
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:
我相信最简单的方法是使用此查询构建一个视图:
然后执行此查询以获取每个用户的最后成绩:
I believe the easiest way is to build a view with this query :
then do this query to get the last grade for each user :