当向他们发送提醒时,我必须为客户列表更新任意数量的行。我可以通过foreach循环来实现这一点,但是我确信必须有一种通过on query做到这一点的方法。
这是当前有效的
foreach($customer_id_arr as $c) mysqli_query($con,"UPDATE crm_customers SET customer_reminded=1 WHERE customer_id=$c");
如何在一个查询中做到这一点?
答案 0 :(得分:5)
您可以利用implode()
来做到这一点:
mysqli_query($con,"UPDATE crm_customers SET customer_reminded=1 WHERE customer_id IN (" . implode(",",$customer_id_arr) . ")";
值得一提的是,您可能不使用prepared statements就可以接受SQL注入。
答案 1 :(得分:1)
不是逃脱数据的最佳方法,但是您可以这样做:
mysqli_query($con,"UPDATE
crm_customers
SET
customer_reminded=1
WHERE customer_id IN (".implode(',', array_map(function($c){return $con->real_escape_string($c);a},$customer_id_arr)).")");
答案 2 :(得分:1)
假设您需要更新客户ID的1、2、3、4和5:
UPDATE crm_customers
SET customer_reminded = 1
WHERE customer_id IN (1, 2, 3, 4, 5)
您甚至可以在IN子句中使用子查询。假设您要在customer_reminded == 0的所有记录上设置customer_reminded = 1。像这样:
UPDATE crm_customers SET customer_reminded = 1
WHERE customer_id IN
(SELECT customer_id
FROM crm_customers
WHERE customer_reminded == 0)