2017-04-22 8 views
0

enter image description hereは、上記の画像から、MySQLのテーブル

から最終とSecondlastのレコードを取得するにはどのように私は持っているn個を持つレコードの数がをCAT_IDと sub_cat_idが、画像では2つだけがあります。

ので、私はlastScorelatetsScoreという名前の通りどのように私はそれを取得することができます。..

最後secondlastscore_in_per値を取得したいですか..?

SELECT 
(SELECT score_in_per FROM tbl_student_skill_score ORDER BY date DESC LIMIT 2,0) as lastScore, 
(SELECT score_in_per FROM tbl_student_skill_score ORDER BY date DESC LIMIT 1) as latetsScore 

私は..

例試みているものを、この複雑なMySQLのlogics..Thisに新しいです: は、一人のユーザーの電子メールは、同じテストを2回を取る[email protected]で、テストがあると言うことができますが1つのカテゴリとサブカテゴリにリンクされています。 ユーザーは何回もテストを受けます...

私は最後の2つのレコードのパーセンテージを取得したいと思います。

+0

サブクエリの問題点は何ですか?あなたはどんな結果を望んでいますか?私は円で囲まれた列が最新のものか二番目のものかはわかりません。 –

+0

その動作しない、時々それは複数の値を取得している内側のクエリ..と今latetsScoreが来るが、最後のスコアはnullとして来ていると –

+0

私は質問に少し明確に説明してみましょう。 –

答えて

0
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 
1

私が正しくあなたを理解していれば、特定の生徒が行った特定のタイプのテストの最新の2つの結果を選択します。

LIMIT節は正しく使用しません。これは正しい構文:LIMIT {[offset,] row_count | row_count OFFSET offset}です。また、where節を完全に除外しました。

ので、クエリは次のようになります。

SELECT 
(SELECT score_in_per FROM tbl_student_skill_score 
    WHERE user_email = "email of the user you are interested in" 
    AND cat_id = categoryOfTestOfInterest 
    AND sub_cat_id = subcategoryOfTestOfInterest 
    ORDER BY date DESC LIMIT 1, 1 
)AS lastScore, 
(SELECT score_in_per FROM tbl_student_skill_score 
    WHERE user_email = "email of the user you are interested in" 
    AND cat_id = categoryOfTestOfInterest 
    AND sub_cat_id = subcategoryOfTestOfInterest 
    ORDER BY date DESC LIMIT 1 
)AS latetsScore; 

(あなたのイメージを示唆するように)学生があなたも(idは常にのために大きいことを想定idで注文する必要がありますよりも、一日の試験を複数回取ることができる場合結果それ以降)またはIDのみで、より良いまだ、この問題を解決する方法の

SELECT 
(SELECT score_in_per FROM tbl_student_skill_score 
    WHERE user_email = "email of the user you are interested in" 
    AND cat_id = categoryOfTestOfInterest 
    AND sub_cat_id = subcategoryOfTestOfInterest 
    ORDER BY id DESC LIMIT 1, 1 
)AS lastScore, 
(SELECT score_in_per FROM tbl_student_skill_score 
    WHERE user_email = "email of the user you are interested in" 
    AND cat_id = categoryOfTestOfInterest 
    AND sub_cat_id = subcategoryOfTestOfInterest 
    ORDER BY id DESC LIMIT 1 
)AS latetsScore; 
1

一つではパーティションを使用することです。
Step1:私は別のcat_idとsub_cat_idのデータを、パーティションごとに日付の降順でランク付けしました。
ステップ2:私は、最新のスコアでランク-1を使用し、最後から二番目のスコア

with chck as 
(select 
    cat_id,sub_cat_id,score_in_per,date1, 
    row_number() over(partition by cat_id,sub_cat_id order by 
    cat_id,sub_cat_id,date1 desc) as row_num 
from tbl) 

    select a.*,b.second_last_score from 
    (select cat_id,sub_cat_id,score_in_per,date1,row_num as last_score from chck where row_num=1) a 
    left join 
    (select cat_id,sub_cat_id,score_in_per,date1,row_num as second_last_score from chck where row_num=2) b 
    on a.cat_id = b.cat_id and a.sub_cat_id = b.sub_cat_id; 

は私が任意のクエリの場合に知らせているランク2でそれを合併しています。

関連する問題