Jump to content

Dynamic Select - Need Advice


threeowadmin

Recommended Posts

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
			";

Link to comment
https://forums.phpfreaks.com/topic/226014-dynamic-select-need-advice/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.