何使用max()获取mysql中的最后一项

时间:2016-03-02 05:35:30

标签: mysql

我希望使用max()获取最后一项作为结果,但即使我使用max(),我也只是获得第一项 enter image description here

以下是SQL代码:

SELECT r.correct, r.items, r.percentage,MAX(r.date_taken) as date_taken,
       u.username,u.FN, u.user_course_type,
       IFNULL(u.user_major_type,'N/A') as user_major_type,u.level_name,
       u.section_name
FROM bcc_fs_exam_result r
INNER JOIN 
(SELECT u.id_user, u.username, CONCAT(u.lastname,', ',u.firstname) as FN,
 c.user_course_type, m.user_major_type, l.level_name, s.section_name
 FROM bcc_fs_user u 
 LEFT JOIN bcc_fs_user_course c on c.id_user_course = u.id_user_course
 LEFT JOIN bcc_fs_user_major m on m.id_user_major = u.id_user_major
 LEFT JOIN bcc_fs_group_level l ON l.id_level = u.id_level
 LEFT JOIN bcc_fs_group_section s ON s.id_section = u.id_section
) u ON r.id_user = u.id_user WHERE r.id_exam = 5 GROUP BY r.id_user 

TIA

1 个答案:

答案 0 :(得分:0)

我从你的问题中得到的是你想获得“正确”,“物品”,“百分比”等。 bcc_fs_exam_result中包含最后或第一个日期的行的列。

如果这是正确的,那么您可以通过首先查找每个bcc_fs_exam_result的最小或最长日期来过滤id_user,然后将其加入到考试结果表中。

SELECT 
    r.correct,
    r.items, 
    r.percentage, 
    r.date_taken,
    u.username, 
    u.FN, 
    u.user_course_type, 
    IFNULL(u.user_major_type,'N/A') as user_major_type, 
    u.level_name, 
    u.section_name 
FROM bcc_fs_exam_result r   
INNER JOIN (

    SELECT u.id_user, 
        u.username, 
        CONCAT(u.lastname,', ',u.firstname) as FN, 
        c.user_course_type, 
        m.user_major_type, 
        l.level_name, 
        s.section_name 
    FROM bcc_fs_user u 
        LEFT JOIN bcc_fs_user_course c on c.id_user_course = u.id_user_course 
        LEFT JOIN bcc_fs_user_major m on m.id_user_major = u.id_user_major 
        LEFT JOIN bcc_fs_group_level l ON l.id_level = u.id_level 
        LEFT JOIN bcc_fs_group_section s ON s.id_section = u.id_section

) u ON r.id_user = u.id_user 

INNER JOIN (    
    SELECT 
        id_user, max(r.date_taken) as last_date_taken
    FROM bcc_fs_exam_result
    GROUP BY id_user
) as lastdate ON  lastDate.id_user = r.id_user and r.date_taken = lastdate.last_date_taken

可以更简单地写成:

SELECT 
    r.correct,
    r.items, 
    r.percentage, 
    r.date_taken,
    u.username, 
    CONCAT(u.lastname,', ',u.firstname) as FN, 
    c.user_course_type, 
    IFNULL(m.user_major_type,'N/A') as user_major_type, 
    l.level_name,
    s.section_name 
FROM bcc_fs_exam_result r   
INNER JOIN (    
    SELECT 
        id_user, max(r.date_taken) as last_date_taken
    FROM bcc_fs_exam_result
    GROUP BY id_user
) as lastdate ON  lastDate.id_user = r.id_user and r.date_taken = lastdate.last_date_taken

INNER JOIN bcc_fs_user u on r.id_user = u.id_user
LEFT JOIN bcc_fs_user_course c on c.id_user_course = u.id_user_course 
LEFT JOIN bcc_fs_user_major m on m.id_user_major = u.id_user_major 
LEFT JOIN bcc_fs_group_level l ON l.id_level = u.id_level 
LEFT JOIN bcc_fs_group_section s ON s.id_section = u.id_section

您假设id_user + date_takenbcc_fs_exam_result的代理键,如果可能的话,您应该使用约束强制执行。否则,它会从您的示例数据中看到唯一ID列id_result的顺序跟在date_taken之后,因此您最好使用Max(id_result)而不是Max(date_taken)。这样可以避免返回一个id_user的重复行,其中两行bcc_fs_exam_result具有相同的taken_date和`id_user。