0

The mysql query below gets me the results I need. The Trouble im having is creating it in php. Im trying to create the two temp tables below from two sets of date range forms then join the results of the temporary tables to display my final result. The dates in the where clauses are where I will use the form fields. The query below works great in mysql. Wondering if anyone can give me a fresh perspective on how i can approach this. Thanks

CREATE TEMPORARY TABLE upsfsttable
SELECT custname,custno,sm,dhlfrt,franchise,date,sublob,resets
FROM upsfreightfstimport
WHERE date > '2014-02-01' and date < '2014-02-28';


CREATE TEMPORARY TABLE sublobrange
SELECT customername,customerno,lob,franchisename,invoicedate,salesrep,items,retail,cost,margin,lineofbiz
FROM sublob2014
WHERE invoicedate > '2014-04-01' and invoicedate < '2014-04-30';

SELECT upsfsttable.custname,upsfsttable.custno,upsfsttable.date,sublobrange.customername,
sublobrange.customerno,sublobrange.invoicedate,sublobrange.items,sublobrange.retail,sublobrange.cost,sublobrange.margin,sublobrange.lineofbiz
FROM upsfsttable
JOIN sublobrange ON upsfsttable.custno = sublobrange.customerno and upsfsttable.dhlfrt = sublobrange.lineofbiz;

<?php

//make first temptable
$maketemp1 = "
 CREATE TEMPORARY TABLE `upsfsttable` (
`fusfstId` int(11) NOT NULL,
`custname` varchar(200) DEFAULT NULL,
`custno` varchar(50) DEFAULT NULL,
`sm` varchar(11) DEFAULT NULL,
`dhlfrt` varchar(11) DEFAULT NULL,
`franchise` varchar(100) DEFAULT NULL,
`date` date DEFAULT NULL,
`sublob` varchar(25) DEFAULT NULL,
`resets` varchar(25) DEFAULT NULL
)
"; 

 mysql_query($maketemp1, $dbhandle) or die ("Sql error : ".mysql_error());

//make second temptable
$maketemp2 = "
 CREATE TEMPORARY TABLE `sublobrange` (
`slid` int(11) NOT NULL,
`customername` varchar(200) DEFAULT NULL,
`customerno` varchar(50) DEFAULT NULL,
`lob` varchar(11) DEFAULT NULL,
`franchisename` varchar(100) DEFAULT NULL,
`invoicedate` date DEFAULT NULL,
`salesrep` varchar(100) DEFAULT NULL,
`items` decimal(18,0) DEFAULT NULL,
`cost` decimal(18,2) DEFAULT NULL,
`retail` decimal(18,2) DEFAULT NULL,
`margin` decimal(18,2) DEFAULT NULL,
`lineofbiz` varchar(25) DEFAULT NULL
)
"; 

mysql_query($maketemp2, $dbhandle) or die ("Sql error : ".mysql_error());

//insert into first temptable 
$inserttemp1 = "
INSERT INTO upsfsttable
  (`custname`, `custno`, `sm`, `dhlfrt`, `franchise`,`date`,`sublob`,`resets`)
SELECT `custname`, `custno`, `sm`, `dhlfrt`, `franchise`, `date`, `sublob`, `resets`
FROM `upsfreightfstimport`
WHERE `date` BETWEEN '2014-02-01' AND '2014-02-28'
";

mysql_query($inserttemp1, $dbhandle) or die ("Sql error : ".mysql_error()); 

//insert into second temptable 

$inserttemp2 = " INSERT INTO sublobrange (customername, customerno, lob, franchisename, invoicedate,salesrep,items,cost,retail,margin) SELECT customername, customerno, lob, franchisename, invoicedate,salesrep,items,cost,retail,margin FROM sublobrange WHERE date BETWEEN '2014-04-01' AND '2014-04-30' ";

mysql_query($inserttemp2, $dbhandle) or die ("Sql error : ".mysql_error()); 

?>

"; } //close the connection mysql_close($dbhandle); ?>
6
  • could you show php version. If this is woking then php version should work too. Aug 20, 2014 at 21:12
  • php version 5.3.13 mysql 5.5.24 here are versions im using
    – david cox
    Aug 20, 2014 at 21:16
  • @davidcox user2102266 was asking for some code. What have you tried in PHP?
    – tlenss
    Aug 20, 2014 at 21:20
  • Yes sorry about being unclear. I tried to ask for not working php code. Aug 20, 2014 at 21:30
  • all good. i appreciate your help. i added php to original post
    – david cox
    Aug 20, 2014 at 21:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.