Unix Technical Forum

Help writing a query

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 ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 08:14 AM
alexp
 
Posts: n/a
Default Help writing a query

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 08:14 AM
PleegWat
 
Posts: n/a
Default Re: Help writing a query

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:58 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com