我有4个变量(年份,月份,项目代码和类型)。PHP MySQL根据变量改变选择查询
因此,如果人提交的年度并保留其他3个变量的空白,然后选择查询必须 select * from table where year(trandate) = $var
但是,如果用户需要提供一年&月份则查询必须select * from table where year(trandate) = $var and month(trandate) = $var1
如果用户选择年月&工程代码并留下类型空白,然后查询必须为select * from table where year(trandate) = $var and month(trandate) = $var1 and projcode = $var3
依此类推。我怎么去编程这个,另外我会有很多组合?
希望问题清楚。
例如这是我迄今为止,但我可以看到,有太多的组合:
if (empty($ej1year) && empty($ej1month) && empty($ej1proj) && empty($ej1type)) {
$rows = mysql_query("SELECT a.employee
,a.trandate
,CONCAT(a.workdone, '-', wc.BriefDescription) as WorkCodeActivity
,CONCAT(a.custname, '-', cl.ShortName) as clientdet
,a.qty
,a.rate
,a.amount
,a.ref
,a.projcode
,a.type
,a.qty*a.rate as costrate
FROM transaction as a
LEFT JOIN workcodes as wc ON a.workdone=wc.WorkCodeNo
LEFT JOIN clients as cl On a.custname=cl.EntityNo");
} elseif (empty($ej1year) && !empty($ej1month)) {
$rows = mysql_query("SELECT a.employee
,a.trandate
,CONCAT(a.workdone, '-', wc.BriefDescription) as WorkCodeActivity
,CONCAT(a.custname, '-', cl.ShortName) as clientdet
,a.qty
,a.rate
,a.amount
,a.ref
,a.projcode
,a.type
,a.qty*a.rate as costrate
FROM transaction as a
LEFT JOIN workcodes as wc ON a.workdone=wc.WorkCodeNo
LEFT JOIN clients as cl On a.custname=cl.EntityNo
where month(trandate) = '$ej1month'");
} elseif
谢谢,会给它一个去,并报告输出 – Wilest