简体   繁体   English

使用时间戳从一个表值更新另一个表

[英]Update another table from one table value with timestamps

I am trying to update a table called "Orders" when a row on another table (called "store") is more than 30 days old. 当另一张表(称为“存储”)上的行已超过30天时,我正在尝试更新一个名为“ Orders”的表。

When a value on "store" is created, it is given a timestamp of the time it was added to the table. 创建“ store”上的值时,将为其添加到表中的时间提供时间戳。 But as soon as that value turns over 30 days old, I need it to get the ID (which is a column that has a unique ID per row) of that cell, and check the table called "orders" for that ID, and update another column where that ID is present. 但是,只要该值超过30天,我就需要它来获取该单元格的ID(每行具有唯一ID的列),并检查该ID的“订单”表并更新该ID存在的另一列。

So far, I've written this, but it is clearly wrong: 到目前为止,我已经写了这个,但这显然是错误的:

<?php

    include ('connectdb.php'); 

    $query = $db->query("SELECT * FROM store WHERE open_time < (NOW()- INTERVAL 30 DAYS)");
    $update = $db->prepare("UPDATE orders SET notified=-1 WHERE unique_id=$query");

    $db = null;

?>

If the tables are on the same database just use a subselect in the WHERE clause, like here: MYSQL UPDATE with IN and Subquery 如果表在同一个数据库中,则只需在WHERE子句中使用子选择,如下所示: 带IN和Subquery的MYSQL UPDATE

Otherwise aggregate the IDs into an array and concatenate them in the WHERE clause with implode . 否则,将ID聚集到一个数组中,并在WHERE子句中将它们与implode连接起来。 Don't forget to quote every ID. 不要忘记引用每个ID。

Also, try to select only the columns you need (unique_id) and not all at once (*). 另外,尝试仅选择所需的列(unique_id),而不是一次选择所有列(*)。

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

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