0

我真的在努力实现这一目标,并希望有人可以帮助我。我有现有的功能和查询:

公共函数 setStockAndPrice($product_id) {
  $query = $this->db->query('UPDATE' . DB_PREFIX . 'product SET 数量 = 0, price = 0.00 WHERE product_id = ' . (int)$product_id)
}

这可行,但是当我实际上希望它仅在该产品存在于另一个表中时将产品设置为零时,它将所有产品设置为零。

即,在解释性方面:

公共函数 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 存在于 CustomerProducts)
}

我不熟悉连接,但我不确定是否需要在这里使用连接,因为查询似乎比这更简单。

谁能指出我正确的方向?

4

3 回答 3

2
  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)" )
  }

这可能有效。

于 2013-04-16T09:27:17.513 回答
0
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');
    }
于 2013-04-16T09:34:15.913 回答
0

使用这将适用于您没有分配db.product并确保您在字符串中编写查询然后执行。

你看到你查询,通过删除评论

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);
}
于 2013-04-16T09:32:07.210 回答