2014-12-02 43 views
2

我是MySQL新手。我需要一个SQL查询以获得以下预期结果。请检查以下,如何使用MySQL获取通过和失败结果的最大日期?

表: user_results

id cId uId tId pass date 
1 23 34 2 Y 7/8/2013 10:24:47 
2 23 34 2 N 11/27/2014 10:36:32 
3 23 34 3 Y 12/9/2013 10:24:47 
4 23 34 3 N 11/27/2014 10:39:10 
5 23 34 4 Y 10/26/2013 10:24:47 
6 23 34 4 N 11/27/2014 10:38:08 
7 59 93 2 Y 11/24/2013 9:34:23 
8 69 82 2 Y 11/28/2014 9:04:22 
9 69 82 2 Y 11/28/2014 8:59:52 
10 69 82 4 Y 11/28/2014 8:59:52 
11 69 82 4 Y 11/28/2014 9:10:40 
12 69 82 4 N 11/28/2014 9:12:01 
13 72 72 2 N 12/1/2014 6:46:02 
14 73 69 2 N 12/1/2014 6:49:29 
15 73 69 3 N 12/1/2014 6:51:31 
16 73 69 3 N 12/1/2014 7:11:25 

下面一个是预期的结果,

id cId uId tId pass date 
1 23 34 2 Y 7/8/2013 10:24:47 
3 23 34 3 Y 12/9/2013 10:24:47 
5 23 34 4 Y 10/26/2013 10:24:47 
7 59 93 2 Y 11/24/2013 9:34:23 
9 69 82 2 Y 11/28/2014 8:59:52 
11 69 82 4 Y 11/28/2014 9:10:40 
13 72 72 2 N 12/1/2014 6:46:02 
14 73 69 2 N 12/1/2014 6:49:29 
16 73 69 3 N 12/1/2014 7:11:25 

注:如果用户通过测试(尝试#),需要显示最近的通过条目,如果用户未通过测试(尝试次数),则需要显示最近的失败条目。

这里是我的查询:

SELECT *, COUNT(tId), MAX(date) 
FROM user_results 
WHERE DATE_ADD(date, INTERVAL 1 YEAR) >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR) 
GROUP BY cId, tId 
HAVING COUNT(tId) =1 OR (pass = 'Y' AND COUNT(tId) >=2) 

请帮帮忙!感谢您的进步!

+0

你的意思是你想要最近的通行证和最近的失败条目 – 2014-12-02 07:39:26

+0

是的,Sunil。我想显示最近的通过和最近的失败条目。 – 2014-12-02 07:43:20

+0

你试过在结果中使用'ORDER BY'吗? – Jhecht 2014-12-02 07:45:19

回答

1

请用下面的查询检查,

1)你必须添加的失败案例在HAVING子句,将获得两通和失败的结果。

2)的毗连用“@”分隔符传和日期列,将得到的值:Y @ 7/8/2013年10时24分47秒

3)该值,你会得到最近排序通过和最近失败。

SELECT *, COUNT(tId), MAX(date), 
SUBSTRING_INDEX(MAX(CONCAT(pass, '@', date)), '@', -1) AS max_date, 
SUBSTRING_INDEX(MAX(CONCAT(pass, '@', date)), '@', 1) AS pass_stat 
FROM user_results 
WHERE DATE_ADD(date, INTERVAL 1 YEAR) >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR) 
GROUP BY cId, tId 
HAVING COUNT(tId) =1 OR (pass = 'Y' AND COUNT(tId) >=2) OR (pass = 'N' AND COUNT(tId) >=2) 
ORDER BY date DESC 
0

试试这个:

SELECT a.id, a.cId, a.uId, a.tI, a.pass, a.date 
FROM tableA a 
INNER JOIN (SELECT a.cId, a.uId, a.tId, MAX(a.date) maxDate 
      FROM tableA 
      GROUP BY a.cId, a.uId, a.tId 
     ) b ON a.cId = b.cId AND a.uId = b.uId AND a.tId = c.tId AND a.date = b.maxDate 
+0

我需要最近的通过和最近的结果失败条目。但在你的查询中,它只显示最近的记录。请检查我的预期结果。 – 2014-12-02 09:19:07

1

查询

select cid, tid, max(date) as max_date 
from user_results where pass = 'Y' group by cId, tId 

给你的所有用户,已经通过了考试和最新的日期。因为我们需要通过cid,tid进行分组,所以我们必须排除id。为了得到正确的ID,以我们的“最终”结果,我们只是做与原来user_results_table与联接:

select r.* from user_results r join 
    (select cid, tid, max(date) as max_date 
    from user_results where pass = 'Y' group by cId, tId) t on 
(r.cId = t.cId and r.tId = t.tId and r.date = t.max_date) 

现在你必须已经通过了最新日期的考试,都喜欢ID附加信息的所有用户和uid。

下一步是包括所有这些用户在考试和最新日期失败。与上面的第一个查询类似,以下查询

select r1.cId, r1.tId, max(r1.date) as max_date 
from user_results r1 
where r1.pass = 'N' 
group by r1.cId, r1.tId 

将为您提供所有未通过考试和最近日期的用户。但是,这个查询的问题在于,它包括每个考试失败的用户,这意味着已通过考试的用户也被包括在内。因此,我们需要通过增加(例如)以exlude谁已经通过的考试,这些用户不存在声明

select r1.cId, r1.tId, max(r1.date) as max_date 
from user_results r1 
where r1.pass = 'N' and not exists 
     (select * from user_results r2 
     where r2.pass = 'Y' and r2.cId = r1.cId and r2.tId = r1.tId) 
group by r1.cId, r1.tId 

就像上面我们再与user_results表加入这个结果让所有的附加信息:

select r.* 
from user_results r join 
    (select r1.cId, r1.tId, max(r1.date) as max_date 
     from user_results r1 
     where r1.pass = 'N' and not exists 
     (select * from user_results r2 
     where r2.pass = 'Y' and r2.cId = r1.cId and r2.tId = r1.tId) 
    group by r1.cId, r1.tId) t on 
(r.cId = t.cId and r.tId = t.tId and r.date = t.max_date) 

最后,我们用union-operator将它们“合并”为结果查询(针对那些已通过考试的用户的查询和针对失败者的查询)。所以最后的查询是:

select r.* 
from user_results r join 
    (select cid, tid, max(date) as max_date 
     from user_results where pass = 'Y' group by cId, tId) t on 
(r.cId = t.cId and r.tId = t.tId and r.date = t.max_date) 
union 
select r.* 
from user_results r join 
    (select r1.cId, r1.tId, max(r1.date) as max_date 
     from user_results r1 
     where r1.pass = 'N' and not exists 
     (select * from user_results r2 
     where r2.pass = 'Y' and r2.cId = r1.cId and r2.tId = r1.tId) 
    group by r1.cId, r1.tId) t on 
(r.cId = t.cId and r.tId = t.tId and r.date = t.max_date) 

这应该有效。我已经测试过,并没有注意到任何失败。

+0

非常感谢你,Iris!它非常清楚地解释。 – 2014-12-02 13:30:00

0

我瘦,这是更简单,做工精细

SELECT * FROM 
(SELECT * 
FROM user_results 
WHERE DATE_ADD(date, INTERVAL 1 YEAR) >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR) 
GROUP BY cId, tId 
ORDER BY date) 
AS a 
ORDER BY CID, tid 
相关问题