如何使用group by获取mysql中的最后两条记录

时间:2017-04-22 18:05:04

标签: mysql sql join group-by subquery

+-------------------------+--------+------------+-------+--------+--------------+------------+----------------------+----+-----------------------------+------------------+
| user_email              | cat_id | sub_cat_id | score | out_of | score_in_per | date       | cat_name             | id | subcat_name                 | supprting_doc_id |
+-------------------------+--------+------------+-------+--------+--------------+------------+----------------------+----+-----------------------------+------------------+
| inststudent@yopmail.com | 1      | 6          | 39    | 40     | 97.5         | 22-04-2017 | IT and Programming   | 1  | Java Programming            | 32               |
+-------------------------+--------+------------+-------+--------+--------------+------------+----------------------+----+-----------------------------+------------------+
| inststudent@yopmail.com | 1      | 5          | 42    | 45     | 93.3         | 22-04-2017 | IT and Programming   | 1  | C Programming               |                  |
+-------------------------+--------+------------+-------+--------+--------------+------------+----------------------+----+-----------------------------+------------------+
| inststudent@yopmail.com | 1      | 5          | 41    | 45     | 91.1         | 22-04-2017 | IT and Programming   | 1  | C Programming               |                  |
+-------------------------+--------+------------+-------+--------+--------------+------------+----------------------+----+-----------------------------+------------------+
| inststudent@yopmail.com | 1      | 6          | 38    | 40     | 95           | 22-04-2017 | IT and Programming   | 1  | Java Programming            | 32               |
+-------------------------+--------+------------+-------+--------+--------------+------------+----------------------+----+-----------------------------+------------------+
| inststudent@yopmail.com | 9      | 11         | 40    | 40     | 100          | 22-04-2017 | Quantitative Ability | 9  | Permutation and Combination | 280              |
+-------------------------+--------+------------+-------+--------+--------------+------------+----------------------+----+-----------------------------+------------------+
| inststudent@yopmail.com | 9      | 11         | 37    | 40     | 92.5         | 22-04-2017 | Quantitative Ability | 9  | Permutation and Combination | 280              |
+-------------------------+--------+------------+-------+--------+--------------+------------+----------------------+----+-----------------------------+------------------+
| inststudent@yopmail.com | 9      | 24         | 35    | 40     | 87.5         | 22-04-2017 | Quantitative Ability | 9  | Time and Distance           | 21,284,274       |
+-------------------------+--------+------------+-------+--------+--------------+------------+----------------------+----+-----------------------------+------------------+

从上表中我想获得最近2条记录lastsecondlast的数据,我需要使用分组依据sub_cat_id来显示列表。

此数据随附此查询:

SELECT tbls.* FROM 
(SELECT scor.user_email,scor.cat_id,scor.sub_cat_id,scor.score,
scor.out_of,scor.score_in_per,scor.date,catg.cat_name,catg.id,
subc.subcat_name,subc.supprting_doc_id 
FROM 
tbl_student_skill_score scor 
LEFT JOIN tbl_skilltest_subcategory subc 
ON scor .sub_cat_id = subc.scat_id 
LEFT JOIN tbl_skilltest_category catg
ON subc.cat_id = catg.id 
where
scor.user_email = 'inststudent@yopmail.com')
as tbls

我想用以下格式添加以下查询:

SELECT 
(SELECT score_in_per FROM tbl_student_skill_score 
WHERE cat_id=1 and sub_cat_id=5 
ORDER BY date,id DESC LIMIT 1 )
AS latestScore,
(SELECT score_in_per FROM tbl_student_skill_score 
WHERE cat_id=1 and sub_cat_id=5 
ORDER BY date,id DESC LIMIT 1,1  ) 
AS lastScore

这就像我的生活会如何来临。

enter image description here

0 个答案:

没有答案