rempires Posted December 29, 2007 Share Posted December 29, 2007 hmm, okay so i have a query and i want to select the eventName based on the biggest number, but MAX apparently can't be used in the WHERE clause, any help, here is what i attempted SELECT `eventName` FROM events WHERE `addedToScheduleCurr` = MAX(`addedToScheduleCurr`) any idea on how else i could do this thanks, john p.s. phpMyAdmin error says #1111 - Invalid use of group function Quote Link to comment https://forums.phpfreaks.com/topic/83652-solved-selecting-highest-field/ Share on other sites More sharing options...
drranch Posted December 30, 2007 Share Posted December 30, 2007 SELECT eventname, MAX(addedToScheduleCurr) FROM events GROUP BY eventname Quote Link to comment https://forums.phpfreaks.com/topic/83652-solved-selecting-highest-field/#findComment-425605 Share on other sites More sharing options...
rempires Posted December 30, 2007 Author Share Posted December 30, 2007 that returns all the events with different names, and if i group by addedToScheduleCurrthen it returns all of the addedToScheduleCurr with different number. I'm attempting to only retrieve the highest addedToScheduleCurr event name and avoid using 2 sql statementS such as SELECT MAX(addedToScheduleCurr) FROM events and then SELECT eventname, FROM events WHERE addedToScheduleCurr = '$resultFromPreviousSQlStatement' Quote Link to comment https://forums.phpfreaks.com/topic/83652-solved-selecting-highest-field/#findComment-425771 Share on other sites More sharing options...
fenway Posted December 30, 2007 Share Posted December 30, 2007 Why can't you just add the where clause the the posted code? Quote Link to comment https://forums.phpfreaks.com/topic/83652-solved-selecting-highest-field/#findComment-425924 Share on other sites More sharing options...
rempires Posted December 30, 2007 Author Share Posted December 30, 2007 mysql returns an error when MAX() function is used in the WHERE statement hmm I'm not quite sure if everyone is understanding what I'm asking either so i'll try to re-explain it lets say i have a table that looks like eventName addedToScheduleCurr event1 10 event2 9 event3 28 event4 15 event5 19 i need an sql statement that will pull out "event3" becuase of the fast that it has the highest value in the addedToScheduleCurr column eventName addedToScheduleCurr event1 15 event2 7 event3 9 event4 35 event5 42 in the above example it would pull out event5 because it has the highest addedToSceduleCurr column i'm starting to think this might have to be 2 separate quires... Quote Link to comment https://forums.phpfreaks.com/topic/83652-solved-selecting-highest-field/#findComment-426171 Share on other sites More sharing options...
rameshfaj Posted December 31, 2007 Share Posted December 31, 2007 I think MAX() requires group by clause also, try that. Quote Link to comment https://forums.phpfreaks.com/topic/83652-solved-selecting-highest-field/#findComment-426642 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 you don't require a group by your query should be SELECT `eventName` FROM events WHERE `addedToScheduleCurr` = (select MAX(`addedToScheduleCurr`) from events) Quote Link to comment https://forums.phpfreaks.com/topic/83652-solved-selecting-highest-field/#findComment-426644 Share on other sites More sharing options...
fataqui Posted December 31, 2007 Share Posted December 31, 2007 You can also do it like this... SELECT eventName FROM events ORDER BY addedToScheduleCurr DESC LIMIT 1; Quote Link to comment https://forums.phpfreaks.com/topic/83652-solved-selecting-highest-field/#findComment-426656 Share on other sites More sharing options...
rempires Posted January 1, 2008 Author Share Posted January 1, 2008 thanks everyone, that was exactly what i was looking for! john Quote Link to comment https://forums.phpfreaks.com/topic/83652-solved-selecting-highest-field/#findComment-427020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.