Author Topic: [SOLVED] selecting highest field  (Read 905 times)

0 Members and 1 Guest are viewing this topic.

Offline rempiresTopic starter

  • Enthusiast
  • Posts: 60
  • Gender: Male
    • View Profile
[SOLVED] selecting highest field
« on: December 29, 2007, 06:03:20 PM »
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

Code: [Select]
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

Offline drranch

  • Enthusiast
  • Posts: 78
    • View Profile
Re: selecting highest field
« Reply #1 on: December 29, 2007, 07:44:02 PM »
Code: [Select]
SELECT eventname, MAX(addedToScheduleCurr) FROM events GROUP BY eventname

Offline rempiresTopic starter

  • Enthusiast
  • Posts: 60
  • Gender: Male
    • View Profile
Re: selecting highest field
« Reply #2 on: December 29, 2007, 11:50:19 PM »
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'

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: selecting highest field
« Reply #3 on: December 30, 2007, 08:54:51 AM »
Why can't you just add the where clause the the posted code?
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline rempiresTopic starter

  • Enthusiast
  • Posts: 60
  • Gender: Male
    • View Profile
Re: selecting highest field
« Reply #4 on: December 30, 2007, 04:27:08 PM »
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...

Offline rameshfaj

  • Enthusiast
  • Posts: 286
  • When the kettle boils it will split over itself.
    • View Profile
Re: selecting highest field
« Reply #5 on: December 31, 2007, 06:31:27 AM »
I think MAX() requires group by clause also, try that.
Ramesh

Offline rajivgonsalves

  • Addict
  • Posts: 2,160
  • Gender: Male
  • Whizzkid
    • View Profile
    • My Personal Website
Re: selecting highest field
« Reply #6 on: December 31, 2007, 06:33:23 AM »
you don't require a group by your query should be


Code: [Select]
SELECT `eventName` FROM events WHERE `addedToScheduleCurr` = (select MAX(`addedToScheduleCurr`) from events)
Cheers!
Rajiv

Code: [Select]
// commenting code... nah!!! if it was hard to write, it should be hard to read :P
echo implode('', array_map('chr', explode(',','87,104,105,122,122,107,105,100')));

Offline fataqui

  • Irregular
  • Posts: 5
    • View Profile
Re: selecting highest field
« Reply #7 on: December 31, 2007, 06:51:59 AM »
You can also do it like this...

Code: [Select]
SELECT eventName FROM events ORDER BY addedToScheduleCurr DESC LIMIT 1;

Offline rempiresTopic starter

  • Enthusiast
  • Posts: 60
  • Gender: Male
    • View Profile
Re: selecting highest field
« Reply #8 on: December 31, 2007, 07:00:38 PM »
thanks everyone, that was exactly what i was looking for!

john