Go Back   Forum Care Forums > Development Reference Area > MySQL Discussion

Reply
 
LinkBack Thread Tools Display Modes
get max/min of subquery
Old
  (#1)
gil
Guest
 
Posts: n/a
Default get max/min of subquery - 06-04-2007, 07:58 AM

hi,

i have a table with a column that is a scalar ("alert_key") .

my subquery is:

SELECT alert_key FROM `user` WHERE alert_key < 4000 ORDER BY alert_key
DESC LIMIT 100

this brings me 100 or less rows. now i need to find the minimum value
from the returned rows and with that value (call it MinValue) do
another SELECT :

SELECT alert_key FROM `user` WHERE alert_key > MinValue ORDER BY
alert_key ASC LIMIT 100

this looks kind of stupid, but the reason for this is that i allways
want to have 100 rows, therefore , if the first SELECT got only 50
rows, i want to take thes 50 rows + 50 more rows that are not <4000.

bottom line: how do i get the MIN of a subquery into a variable , all
in one statement.



thanks a lot,

gil

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: get max/min of subquery
Old
  (#2)
lark
Guest
 
Posts: n/a
Default Re: get max/min of subquery - 06-04-2007, 07:58 AM

gil wrote:
> hi,
>
> i have a table with a column that is a scalar ("alert_key") .
>
> my subquery is:
>
> SELECT alert_key FROM `user` WHERE alert_key < 4000 ORDER BY alert_key
> DESC LIMIT 100
>
> this brings me 100 or less rows. now i need to find the minimum value
> from the returned rows and with that value (call it MinValue) do
> another SELECT :
>
> SELECT alert_key FROM `user` WHERE alert_key > MinValue ORDER BY
> alert_key ASC LIMIT 100
>
> this looks kind of stupid, but the reason for this is that i allways
> want to have 100 rows, therefore , if the first SELECT got only 50
> rows, i want to take thes 50 rows + 50 more rows that are not <4000.
>
> bottom line: how do i get the MIN of a subquery into a variable , all
> in one statement.
>
>
>
> thanks a lot,
>
> gil
>


have you tried something like this:

SELECT alert_key FROM `user` WHERE alert_key > (SELECT min(alert_key)
FROM `user` WHERE alert_key < 4000) ORDER BY
alert_key ASC LIMIT 100


--
lark -- EMAIL REMOVED
To reply to me directly, delete "despam".
   
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top