Mysql获取行,直到值等于某个值

时间:2015-04-20 12:36:19

标签: mysql sql

我们有这样的查询:

SELECT u.email, st.value as score,
date_format(FROM_UNIXTIME(st.timemodified),'%d-%m-%Y %H:%i:%s' ) as fecha,
IF(st.value >= 70, 'YES', 'NO') as result
FROM user u
LEFT JOIN scorm_scoes_track st ON st.userid = u.id 
LEFT JOIN scorm_scoes as ss ON ss.scorm = st.scormid
WHERE (st.element='cmi.score.raw' OR st.element='cmi.core.score.raw') 
ORDER BY u.id, st.timemodified

+------------------+------------+---------------------+----------+
| email            | score      | fecha               | result   |
+------------------+------------+---------------------+----------+
| test@test.es     | 45         | 14-03-2015 17:10:18 | NO       |
| test@test.es     | 65         | 14-03-2015 17:12:42 | NO       |
| test@test.es     | 70         | 14-03-2015 17:15:04 | YES      |
| test@test.es     | 60         | 14-03-2015 17:17:16 | NO       |
| test@test.es     | 65         | 14-03-2015 17:17:16 | NO       |
+------------------+------------+---------------------+----------+

如果结果大于/等于70,则用户通过测试。我们只需要显示结果直到70(通过测试时)。

在这种情况下,我们需要这个结果:

| test@test.es     | 45         | 14-03-2015 17:10:18 | NO       |
| test@test.es     | 65         | 14-03-2015 17:12:42 | NO       |
| test@test.es     | 70         | 14-03-2015 17:15:04 | YES      |

因为得分后的结果> = 70必须放弃

提前致谢!

2 个答案:

答案 0 :(得分:0)

st.value添加到WHERE子句。

SELECT u.email, st.value as score,
date_format(FROM_UNIXTIME(st.timemodified),'%d-%m-%Y %H:%i:%s' ) as fecha,
IF(st.value >= 70, 'YES', 'NO') as result
FROM user u
LEFT JOIN scorm_scoes_track st ON st.userid = u.id 
LEFT JOIN scorm_scoes as ss ON ss.scorm = st.scormid
WHERE (st.element='cmi.score.raw' OR st.element='cmi.core.score.raw') AND st.value >= 70 
ORDER BY u.id, st.timemodified

答案 1 :(得分:0)

为什么不添加 WHERE 语句?

SELECT u.email, st.value as score,
date_format(FROM_UNIXTIME(st.timemodified),'%d-%m-%Y %H:%i:%s' ) as fecha,
IF(st.value >= 70, 'YES', 'NO') as result
FROM user u
LEFT JOIN scorm_scoes_track st ON st.userid = u.id 
LEFT JOIN scorm_scoes as ss ON ss.scorm = st.scormid
WHERE (st.element='cmi.score.raw' OR st.element='cmi.core.score.raw')
AND st.value >= 70
ORDER BY u.id, st.timemodified

我刚在原始查询中添加 AND st.value> = 70