SELECT *
FROM channel_marketing
WHERE (p_type =colname) OR (c_type =comtype) OR (c_name =comname) OR (year=s_year) OR (month =s_month) OR (week =s_week)
其實我的做法跟你最後的做法類似,只是可以精減一下..
複製程式
$sqlwh='';
if ($colname) $sqlwh.=' OR p_type='$colname';
if ($comtype) $sqlwh.=' OR c_type='$comtype';
if ($comname) $sqlwh.=' OR c_name='$comname';
if ($s_year && $s_month) $sqlwh.=' OR (year='$s_year' AND month='$s_month')';
elseif ($s_year) $sqlwh.=' OR year='$s_year';
$sql = "SELECT * FROM sales WHERE 0 $sqlwh ORDER BY year ASC, month ASC";