在PHP中连接Mysql查询

时间:2012-12-04 17:47:19

标签: php mysql concatenation

我在连接此查询时遇到问题:

$sql.=' WHERE ticket.dept_id IN('.implode(',',$thisuser->getDepts()).') OR ticket.staff_id='.db_input($thisuser->getId());

用这个:

AND ticket.brand_id IN('.implode(',',$thisuser->getBrands())

我怎么能做到这一点?

1 个答案:

答案 0 :(得分:1)

你可以在同一行连接字符串:

$sql.=' WHERE ticket.dept_id IN('.implode(',',$thisuser->getDepts()).') OR ticket.staff_id='.db_input($thisuser->getId()) . ' AND ticket.brand_id IN('.implode(',',$thisuser->getBrands()) . ')';

或者您可以在第二行执行此操作:

$sql.=' WHERE ticket.dept_id IN('.implode(',',$thisuser->getDepts()).') OR ticket.staff_id='.db_input($thisuser->getId());
$sql.=' AND ticket.brand_id IN('.implode(',',$thisuser->getBrands()) . ')';

但是,由于您的原始WHERE子句包含OR,因此您必须确保保持逻辑完整。我建议在连接额外的AND子句之前将前两个条件包装在一组括号中:

$sql.=' WHERE (ticket.dept_id IN('.implode(',',$thisuser->getDepts()).') OR ticket.staff_id='.db_input($thisuser->getId()) . ')';
$sql.=' AND ticket.brand_id IN('.implode(',',$thisuser->getBrands()) . ')';