This is a discussion on Help writing a query within the MySQL forums, part of the Database Server Software category; --> Hi, I would be greatful if someone could help me in writing a query. I have a table as ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi, I would be greatful if someone could help me in writing a query. I have a table as follows: NAME AGE Bob 44 Mary 59 James 32 I was wondering if I could select the row that had the nearest age to the one i specify. For example I could specify an age of 43. In this case the top row (Bob's info) would be returned as his age was the nearest to the age of 43 in the query. Thanks |
| ||||
| In article <1159197155.186851.167430@b28g2000cwb.googlegroups .com>, alexp says... > Hi, I would be greatful if someone could help me in writing a query. > > I have a table as follows: > > NAME AGE > Bob 44 > Mary 59 > James 32 > > I was wondering if I could select the row that had the nearest age to > the one i specify. > > For example I could specify an age of 43. In this case the top row > (Bob's info) would be returned as his age was the nearest to the age of > 43 in the query. > > Thanks SELECT `NAME`,`AGE` FROM `table` WHERE `AGE` = ( SELECT MIN(ABS(`age`-43)) FROM `table` ); -- PleegWat Remove caps to reply |