Hi all, I am trying to select single record from each year of last ten year. Given below is the code what I am using now. But its taking too much time to get the result. Is there any way to make it fast?
$sql_cond ="SELECT Close_Price FROM (SELECT Close_price FROM cash_data WHERE SERIES='".$series."' AND SYMBOL='".$symbol."'";
$sql_gain_data="SELECT
(".$sql_cond." AND date_added <= '".$year_1."' ORDER BY date_added DESC LIMIT 0,1) as year1) as clyear1,
(".$sql_cond." AND date_added <= '".$year_2."' ORDER BY date_added DESC LIMIT 0,1) as year2) as clyear2,
(".$sql_cond." AND date_added <= '".$year_3."' ORDER BY date_added DESC LIMIT 0,1) as year3) as clyear3,
(".$sql_cond." AND date_added <= '".$year_4."' ORDER BY date_added DESC LIMIT 0,1) as year4) as clyear4,
(".$sql_cond." AND date_added <= '".$year_5."' ORDER BY date_added DESC LIMIT 0,1) as year5) as clyear5,
(".$sql_cond." AND date_added <= '".$year_6."' ORDER BY date_added DESC LIMIT 0,1) as year6) as clyear6,
(".$sql_cond." AND date_added <= '".$year_7."' ORDER BY date_added DESC LIMIT 0,1) as year7) as clyear7,
(".$sql_cond." AND date_added <= '".$year_8."' ORDER BY date_added DESC LIMIT 0,1) as year8) as clyear8,
(".$sql_cond." AND date_added <= '".$year_9."' ORDER BY date_added DESC LIMIT 0,1) as year9) as clyear9,
(".$sql_cond." AND date_added <= '".$year_10."' ORDER BY date_added DESC LIMIT 0,1) as year10) as clyear10";
Thanks.