threeowadmin Posted January 29, 2011 Share Posted January 29, 2011 Hey everyone, thanks in advance for viewing my post. Any insight you might have would be great. I need to figure out the best way to create a select statement which dynamically selects from one of two tables based on a date value. I can do this in php but for performance purposes i am constrained to solve it using sql only. The sql statement i need to modify is shown below. I need to switch in between tblfacprice and tbloldfacprice based on tickethdrid.datecreated. Any ideas? $sql = "select ponumberline , tbltickethdr.tickethdrid , tbltickethdr.datecreated , tbltickethdr.distributorid as distributorid1 , tblticketline.startdate as linestartdate , tblticketline.enddate as lineenddate , tblticketline.invoicedthrudate , tblticketline.invoicedthrudate as lineinvoicedthru , tblticketline.catalogid , tblticketline.catalogid as cid , tblticketline.* , tbltickethdr.* , tblcatalog.* , tblfacprice.* from tblticketline join tbltickethdr using(tickethdrid) join tblcatalog using (catalogid) left join tblfacprice on (tblcatalog.catalogid = tblfacprice.catalogid and facagreementid = $agreementid) where facilityid = $facilityid and tbltickethdr.tickettype <> 'consignment' and (tblticketline.invoicedthrudate is null or (tblticketline.invoicedthrudate <= '".changeDateFormateInMysqlSupport($enddate) ."' and tblticketline.invoicedthrudate<coalesce(tblticketline.enddate, now()))) and tblticketline.startdate <= '" .changeDateFormateInMysqlSupport($enddate)."' and tblticketline.voided is null and tblticketline.latevoided is null and tbltickethdr.voided is null and tblticketline.datecreated>='2006-01-01' and (tbltickethdr.invoicedthrudate is null or tbltickethdr.invoicedthrudate < '" .changeDateFormateInMysqlSupport($enddate) . "') order by tbltickethdr.distributorid "; Quote Link to comment https://forums.phpfreaks.com/topic/226014-dynamic-select-need-advice/ Share on other sites More sharing options...
sunfighter Posted January 30, 2011 Share Posted January 30, 2011 php uses sql to query the database. Everything inside of () is sql in a mysql_query. So if you can do it in php just use the same code for sql. Quote Link to comment https://forums.phpfreaks.com/topic/226014-dynamic-select-need-advice/#findComment-1167396 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.