vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| If I have a dataset as below: Name, Age, Word ---------------------------- Bob, 13, bill Joe, 13, oxo Alex, 14, thing Jim, 14, blob Phil, 14, whatsit Ben, 15, doodah Rodney, 15, thingy I want to select the first block where the age is equal, i.e. return in the case of the above set, Bob and Joe, and with the same query if Bob and Joe didn't exist, return Alex, Jim, and Phil. In broken SQL, I want to "SELECT * FROM `table` WHERE 'Age'='Age 1st Row';". I'm guessing this could be done with nested queries, but this is me attempting to optimise a script from 3 queries to 1, so the gains by using nested queries would be negligible. I'm guessing that this isn't possible, but I thought asking some people that know more than myself couldn't hurt. Any assistance is appreciated. Thanks, Adam Bishop |
| |||
| In the last episode (Jan 22), Adam Bishop said: > If I have a dataset as below: > > Name, Age, Word > ---------------------------- > Bob, 13, bill > Joe, 13, oxo > Alex, 14, thing > Jim, 14, blob > Phil, 14, whatsit > Ben, 15, doodah > Rodney, 15, thingy > > I want to select the first block where the age is equal, i.e. return > in the case of the above set, Bob and Joe, and with the same query if > Bob and Joe didn't exist, return Alex, Jim, and Phil. > > In broken SQL, I want to "SELECT * FROM `table` WHERE 'Age'='Age 1st Row';". How about: SELECT * FROM mytable WHERE Age=MIN(age); The smallest and largest values for a column are alawys available via MIN() and MAX(). If you had wanted the 2nd smallest, or the top 3, then you would have needed a subquery. -- Dan Nelson dnelson@allantgroup.com |
| ||||
| Ah, that would work. Looks like I was making the problem too complex in my mind, thanks for your help. Adam Bishop -----Original Message----- From: Dan Nelson [mailto:dnelson@allantgroup.com] Sent: 22 January 2007 07:07 To: Adam Bishop Cc: mysql@lists.mysql.com Subject: Re: SQL Query Question In the last episode (Jan 22), Adam Bishop said: > If I have a dataset as below: > > Name, Age, Word > ---------------------------- > Bob, 13, bill > Joe, 13, oxo > Alex, 14, thing > Jim, 14, blob > Phil, 14, whatsit > Ben, 15, doodah > Rodney, 15, thingy > > I want to select the first block where the age is equal, i.e. return > in the case of the above set, Bob and Joe, and with the same query if > Bob and Joe didn't exist, return Alex, Jim, and Phil. > > In broken SQL, I want to "SELECT * FROM `table` WHERE 'Age'='Age 1st Row';". How about: SELECT * FROM mytable WHERE Age=MIN(age); The smallest and largest values for a column are alawys available via MIN() and MAX(). If you had wanted the 2nd smallest, or the top 3, then you would have needed a subquery. -- Dan Nelson dnelson@allantgroup.com -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/mysql?unsub=adam@omega.org.uk |
| Thread Tools | |
| Display Modes | |
|
|