using group by and order by in mysql query correctly
03:23 19 Apr 2016

I'm trying to get the latest certificate a user has from the database. I only want to see the latest one and not all the others so I'm using group by and then ordering by the unique id from the main table.

Without group by this works perfectly. I see the last certificate uploaded and then all the others below.

As soon as I add the group by I see the first certificate ever uploaded which is pointless as it could be from years ago.

My query is quite large as I'm drawing in a lot of other information from other tables.

Here is my query.

    SELECT 
        usercert.*, 
        cert.*,
        certCat.certCatName,
        certTask.certTaskName ,
        certStatus.certStatusName 
    FROM 
        `usercert`
    INNER JOIN 
        cert 
    ON 
        cert.idcert = usercert.idcert
    INNER JOIN 
        certCat 
    ON 
        certCat.idcertCat = cert.idcertCat
    INNER JOIN 
        certTask 
    ON 
        certTask.idcertTask = usercert.idcertTask
    INNER JOIN  
        certStatus 
    ON 
        certStatus.idcertStatus =   usercert.idcertStatus
    WHERE  
        usercert.iduser=%s 
    GROUP BY 
        usercert.idcert  
    ORDER BY 
        usercert.usercertEnd DESC
mysql