如何使用数据库中的另一个表更新表记录?

时间:2011-12-05 10:22:07

标签: php mysql sql

表一:products其中包含

等字段
products_id
products_price
master_categories_id

表二:specials。此表为空,其中的字段为

specials_id (an  autoincrement field)
products_id (the value same as table one)
specials_new_products_price (the value equals products_price-(products_price*0.05))
specials_date_added (the value like this 2011-12-05 12:17:44)
specials_last_modified (the value like this  2011-12-05 12:19:10)
expires_date  (2011-12-31)
date_status_change  (the value like this 2011-12-05 12:19:10)
status  (1) 
specials_date_available  (2011-11-29)

现在,我想插入数据

("SELECT products_id,products_price FROM products where master_categories_id=79")
into table 2. and set the `status=1, specials_date_available=2011-11-29, 
expires_date=2011-12-31`.`products_id(the value same as table one)

specials_new_products_price (the value equals products_price-(products_price*0.05)) 

其余字段的值取决于您。

我认为哪种方式不好,写一个php文件来做插入。选择products_id,products_price,然后将它们放在一行接一行的txt文件中,然后将文件内容放入表2.但我不知道如何将文本文件数据放入table2以及如何设置休息领域的价值?

1 个答案:

答案 0 :(得分:0)

insert into specials
(products_id, products_price,status,specials_date_available,expires_date)
SELECT products_id,products_price*0.95 as products_price,
 1, '2011-11-29', '2011-12-31'
FROM products where master_categories_id=79

或类似的东西。