下面的mysql查询得到了我需要的结果。麻烦我正在用PHP创建它。我试图从两组日期范围表单创建下面的两个临时表,然后加入临时表的结果以显示我的最终结果。 where子句中的日期是我将使用表单字段的位置。下面的查询在mysql中运行良好。想知道是否有人能给我一个新的视角,我可以如何处理这个问题。感谢
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
,{ {1}},retail
)
选择margin
,customername
,customerno
,lob
,franchisename
,invoicedate
,salesrep
,items
,{ {1}},cost
来自retail
margin
在'2014-04-01'与'2014-04-30'之间sublobrange
“;
date
&GT;
“; } //关闭连接 mysql_close($数据库句柄); ?&GT;