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