[英]Update one table where record found in another table
I am really struggling on how to achieve this and hope that someone can help me. 我真的在努力实现这一点,并希望有人能帮助我。 I have the existing function and query: 我有现有的功能和查询:
public function setStockAndPrice($product_id) { $query = $this->db->query('UPDATE ' . DB_PREFIX . 'product SET quantity = 0, price = 0.00 WHERE product_id = ' . (int)$product_id) }
This works, but it sets all products to Zero when I actually want it to only set products to Zero when that product exists in another table. 这样可行,但是当我确实希望它只在该产品存在于另一个表中时将产品设置为零时,它会将所有产品设置为零。
ie, in explanatory terms: 即,在解释性方面:
public function setStockAndPrice($product_id) { $query = $this->db->query('UPDATE ' . DB_PREFIX . 'product SET quantity = 0, price = 0.00 WHERE product_id = ' . (int)$product_id AND product_id exists in CustomerProducts) }
I am not au fait with joins, but I am not sure if I even need to use a join here as the query seems more simple than that. 我不喜欢加入,但我不确定我是否需要在这里使用连接,因为查询似乎比这更简单。
Can anyone point me in the right direction? 谁能指出我正确的方向?
public function setStockAndPrice($product_id) {
$query = $this->db->query('UPDATE ' . DB_PREFIX . 'product SET quantity = 0, price = 0.00 WHERE product_id = ' . (int)$product_id ." AND product_id =(select DISTINCT(product_id) from CustomerProducts where product_id= $product_id)" )
}
This may work. 这可能有效。
use this will work for you are not assigning db.product
and make sure you write query in a string then execute. 使用这将适用于您不分配db.product
并确保您在字符串中写入查询然后执行。
And you see you query, by removing comments 你看到你通过删除评论来查询
public function setStockAndPrice($product_id) {
$query_string = "UPDATE " . DB_PREFIX . ".product SET quantity = '0', price = '0.00' WHERE product_id = '$product_id'";
// echo "Query : " . $query_string;
$query = $this->db->query($query_string);
}
public function setStockAndPrice($product_id) {
$query = $this->db->query('UPDATE ' . DB_PREFIX . '.product p, ' . DB_PREFIX . '.CustomerProducts cp SET p.quantity = 0, p.price = 0.00 WHERE p.product_id = ' . (int)$product_id . ' AND p.product_id = cp.product_id');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.