简体   繁体   English

如何设置表中的一列,而另一表中的列具有特定值?

[英]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. 我有两个表,一个表包含很多产品信息,例如product_name product_id等,另一个表包含product_id的列表以及该产品在何处标记为正在销售。 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. 我希望能够创建可以通过Chron作业定期运行的内容,该作业将查看table1.product_discount_id,并且如果产品在该列中除0以外的任何内容,它将table2.product_on_sale更新为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: 我在理解如何确保只更改折扣ID为0以外的其他商品的字段时遇到了麻烦。我在php脚本中已经做到了这一点:

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. 这将生成我希望将table2.product_on_sale字段更改为yes的产品的product_id列表。 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. 然后,我想说一下先前检索的产品ID是否与table2.product_on_sale字段中的product_id匹配,然后更改为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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table? 如何从另一个表中没有特定值的UID的表中选择*? - How can I select * from a table where a UID doesn't exist with a certain value in another table? 从一个表中选择表行,其中另一个表中的表列的值为x - Selecting table rows from one table where value of table column in another table is x 如何将每列中的 2 个值输入到另一个表中的一列 - how to input 2 value from each column to one column in another table 连接表,其中一个表中的字段值是另一表中的列名 - Join table where field value in one table is a column name in another table 如何在表格中标记一列 - How can I mark one column in a table 如何将一个MySql表中的值与PHP中另一个表的列名匹配? - How do I match value from one MySql table to column name of another table in PHP? 我如何插入到table1中,其中table2.column = value? - How can i insert into Table1 where table2.column = value? 我需要从一个表中获取记录,其中列值将以“某些值”开头,并以“另一种值”结尾 - i need to fetch records from one table where column value will be start with 'some value' and end with 'another some value' 如何在不同的表列中获取另一个表数据? - How can i fetch another table data in different table column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM