2017-04-24 46 views
1
+----+-------------------------+--------+------------+-------+--------+--------------+------------+ 
| id |  user_email  | cat_id | sub_cat_id | score | out_of | score_in_per | date  | 
+----+-------------------------+--------+------------+-------+--------+--------------+------------+ 
| 13 | [email protected] | 9  | 11   | 40 | 40  | 100   | 22-04-2017 | 
+----+-------------------------+--------+------------+-------+--------+--------------+------------+ 
| 14 | [email protected] | 9  | 11   | 37 | 40  | 92.5   | 22-04-2017 | 
+----+-------------------------+--------+------------+-------+--------+--------------+------------+ 
| 26 | [email protected] | 9  | 11   | 36 | 40  | 88   | 23-04-2017 | 
+----+-------------------------+--------+------------+-------+--------+--------------+------------+ 
| 27 | [email protected] | 9  | 11   | 35 | 40  | 80   | 23-04-2017 | 
+----+-------------------------+--------+------------+-------+--------+--------------+------------+ 

從上表我想這樣的記錄誰能幫助我/如何獲取最後和最新的記錄。以及如何在子查詢中使用它。

+-----------+-----------------+---------------+-------------------------+--------+------------+-------+--------+--------------+------+ 
| lastScore | secondLastScore | maxPortaScore |  user_email  | cat_id | sub_cat_id | score | out_of | score_in_per | date | 
+-----------+-----------------+---------------+-------------------------+--------+------------+-------+--------+--------------+------+ 
| 80  | 88    | 100   | [email protected] | 9  | 11   | -  | -  | -   | - | 
+-----------+-----------------+---------------+-------------------------+--------+------------+-------+--------+--------------+------+ 

這是我的查詢:

SELECT 
scor.id, 
(SELECT score_in_per 
FROM tbl_student_skill_score 
WHERE cat_id = scor.cat_id and sub_cat_id = scor.sub_cat_id and scor.user_email='[email protected]' 
ORDER BY scor.date DESC,scor.id DESC LIMIT 1) 
as lastScore, 

(SELECT score_in_per 
FROM tbl_student_skill_score 
WHERE cat_id=scor.cat_id and sub_cat_id=scor.sub_cat_id and scor.user_email='[email protected]' 
ORDER BY scor.date DESC,scor.id DESC LIMIT 1,1) 
as secondLastScore, 

(SELECT max(cast(score_in_per as decimal(5,2))) 
FROM tbl_student_skill_score 
WHERE cat_id = scor.cat_id and sub_cat_id=scor.sub_cat_id) 
as maxPortaScore, 

scor.user_email,scor.cat_id, 
scor.sub_cat_id,scor.score, scor.out_of, 
scor.score_in_per,scor.date 
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 = '[email protected]' 
GROUP BY 
sub_cat_id 
ORDER by 
scor.id DESC,scor.date DESC 

形成上述查詢lastScoresecondLastScore不工作

lastScore意味着我的情況從第一張表中最後存儲的記錄是id:27記錄。所以結果應該是

同樣

secondLastScore意味着在我的情況下,從第一個表是id的倒數第二個存儲的記錄:26的紀錄。所以結果應該是

maxPortaScore意味着橫跨表該特定類別的最大得分它不涉及在我的情況下,特定的學生是 我已經使用相同的用戶爲但實際上它可以是任何用戶評分。 [這工作是絕對精細]

+1

如果用戶(電子郵件)只有一條記錄會怎麼樣?那麼什麼是'secondLastScore'值? –

+0

'secondLastScore'將會是空白的......很好的問題btw @OtoShavadze –

+0

學生可能只有一個'cat_id'和'sub_cat_id'? –

回答

1

該查詢似乎是有點複雜的,這裏我選擇使用更少加入做到這一點:

select 
    (
     select t1.score_in_per 
     from tbl_student_skill_score t1 
     where t1.user_email = t.user_email 
     and t1.cat_id = t.cat_id 
     and t1.sub_cat_id = t.sub_cat_id 
     and t1.id = max(t.id) -- this will get the max(id) record in each group 
     limit 1 
    ) lastScore, 
    (
     select t1.score_in_per 
     from tbl_student_skill_score t1 
     where t1.user_email = t.user_email 
     and t1.cat_id = t.cat_id 
     and t1.sub_cat_id = t.sub_cat_id 
     and t1.id < max(t.id) -- this will get all the record which id less than max(id), then use `order by t1.id desc limit 1`, of course the second last record will be retrieved. 
     order by t1.id desc 
     limit 1 
    ) secondLastScore, 
    max(score_in_per) as maxPortaScore, 
    user_email, `cat_id`, `sub_cat_id` 
from tbl_student_skill_score t 
group by `user_email`, `cat_id`, `sub_cat_id` 

一些其他的連接,您應該自行添加。
如果每組中只有一條記錄,則secondLastScore將爲NULL

+0

它工作起來像一個魔法,你可以告訴我在哪裏可以學習所有的SQL主題well..the你寫代碼的方式看起來很棒。 thnkx。 –

+1

在哪裏學習,我不確定,如果有的話,我認爲這只是更多的練習,更多的思考,如果你想,這裏也可以給你更多益處。@ AbdulWaheed – Blank

1

你能做到這一點的三個步驟:首先你得到的最後一個ID,併爲每個用戶

select user_email, max(id) max_id, max(score_in_per) max_score 
from tbl_student_skill_score 
group by user_email 

然後你得到的倒數第二個ID的最大比分通過上面的查詢

select t2.max_id, max(id) as second_max, t2.max_score, user_email 
from tbl_student_skill_score t1 
right join (
      select user_email, max(id) max_id, max(score_in_per) max_score 
      from tbl_student_skill_score 
      group by user_email 
     ) t2 
on  t1.user_email = t2.user_email 
where t1.id < t2.max_id 
group by user_email 

最後,你可以加入這一切都與原來的表來獲得類別信息,並與去年和倒數第二個ID

相關的分數加入表
select t2.score_in_per as lastScore, 
     t3.score_in_per as secondLastScore, 
     t1.max_score as maxPortaScore, 
     t2.user_email, 
     t2.cat_id, 
     t2.sub_cat_id 
from (
      select t2.max_id, max(id) as second_max, t2.max_score, user_email 
      from tbl_student_skill_score t1 
      right join (
         select user_email, max(id) max_id, max(score_in_per) max_score 
         from tbl_student_skill_score 
         group by user_email 
        ) t2 
      on  t1.user_email = t2.user_email 
      where t1.id < t2.max_id 
      group by user_email 
     ) t1 
join tbl_student_skill_score t2 
on  t1.user_email = t2.user_email and 
     t1.max_id = t2.id 
left join 
     tbl_student_skill_score t3 
on  t1.user_email = t3.user_email and 
     t1.second_max = t3.id 
相關問題