简体   繁体   中英

How can I set one column in a table where a column in another table has a certain value?

I have two tables, one contains a lot of product information such as product_name product_id etc and the other contains a list of product_ids and where or not that product is marked as on sale. I would like to be able to create something that I can run regularly through a Chron job which will look at table1.product_discount_id and if the product has anything in that column other than 0 it will update table2.product_on_sale to yes. I am having trouble with understanding how to make sure that I only alter the fields for the products with a discount id of anything other than 0. I have got this far in the php script:

mysql_connect("myhost", "mydb", "mypassword") or die(mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT * FROM jos_vm_product WHERE product_discount_id != '0'");
while($row = mysql_fetch_array($result))
  {
 echo $row['product_id'];
  echo ",";
  }

and that will produce a list of the product_ids for the products I wish to change the table2.product_on_sale field to yes. I'd like to then say if the product ids previously retrieved match a product_id in table2.product_on_sale field then change to yes. Any help would be greatly appreciated.

Regards Ali.

您不应该使用Cron作业进行更新,而应使用select中的联接来查找要出售的商品

mysql_query("update table1, table2 set table2.product_on_sale=true where table1.product_id=table2.product_id and table1.product_discount_id!=0")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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