繁体   English   中英

更新一个表,其中记录在另一个表中找到

[英]Update one table where record found in another table

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

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

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

即,在解释性方面:

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

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

谁能指出我正确的方向?

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

这可能有效。

使用这将适用于您不分配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);
}
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM