views:

60

answers:

1

This query method works :

SELECT xxx 
FROM xxx-table 
WHERE YEAR( CURRENT_DATE( ) ) - YEAR( '1988-10-27' ) 
                        - ( DATE_FORMAT( CURRENT_DATE( ) , '%m-%y' ) < '12-31' ) 
                                  Between 20 and 25 

But what I need is something like this...

SELECT YEAR( CURRENT_DATE( ) )
                     - YEAR( '(Select xxx 
                                From xxx-table 
                                where year_id=9 limit 1)-10-27' ) 
                                 - ( DATE_FORMAT( CURRENT_DATE( ) , '%m-%y' )
                           < '12-31' ) 
        Between 20 and 25 

I need to extract value from the database and process it using a sub queries but it doesn't work. I guess sub queries aren't allow in function. Is there a work around on this?

Thanks in advance!

+1  A: 
  1. You've got your subselect in quotes, so it's treated as a string.
  2. There's really no need to append the month and day to the year that you've just picked out in the subquery, since you're then running a year function on it.

Try this:

SELECT YEAR( CURRENT_DATE( ) )
                     - (Select xxx
                                From xxx-table
                                where year_id=9 limit 1)
                                 - ( DATE_FORMAT( CURRENT_DATE( ) , '%m-%y' )
                           < '12-31' )
        Between 20 and 25
lins314159