SQL error retrieving a specific row

I have a question about my sql query that seems to be playing.

I am completely ready to admit any mistake in the code that I use, but I can’t mind that my life works on what happens.

The table below is:

id groupingid groupid timeadded ------------------------------------------ 1 1 4 1318200332 2 2 2 1318200346 3 3 1 1318200362 4 4 3 1318200388 5 5 6 1318217398 6 7 7 1318217402 7 8 8 1318217408 8 6 5 1318217413 9 9 9 1318217417 10 5 7 1319007223 65 14 14 1319068963 64 11 15 1319068950 62 10 15 1319068921 63 13 14 1319068950 61 12 14 1319068887 60 11 14 1319068850 59 10 14 1319068847 70 12 15 1319069000 

All I want to do is return any row with "groupid" of 15, and then isolate the row with the highest "groupingid"

so i use the following sql_query

 SELECT * FROM `mdl_groupings_groups` WHERE groupid = 15 ORDER BY MAX('groupingid') 

He keeps coming back:

 id groupingid groupid timeadded ------------------------------------------ 64 11 15 1319068950 

whereas it should return a string with identifier 70

Please tell me that it is a TOTALLY USUAL thing that I am missing because I am in tight deadline and do not know what is wrong here.

We apologize if this is incorrectly formatted and flame if necessary ..

thanks

+4
source share
2 answers

Two things:

  • Use ORDER BY ... DESC instead of ORDER BY MAX(...)
  • Do not put the groupingid column name in single quotes.

Try the following:

 SELECT * FROM mdl_groupings_groups WHERE groupid = 15 ORDER BY groupingid DESC LIMIT 1 
+6
source

Try the following:

 select * from mdl_groupings_group WHERE groupingID IN ( SELECT max(groupingID) as TheMax FROM `mdl_groupings_groups` WHERE groupid = 15 ) xx ORDER BY groupingID DESC LIMIT 1 
0
source

All Articles