简体   繁体   中英

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.

When a value on "store" is created, it is given a timestamp of the time it was added to the table. 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.

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

Otherwise aggregate the IDs into an array and concatenate them in the WHERE clause with implode . Don't forget to quote every ID.

Also, try to select only the columns you need (unique_id) and not all at once (*).

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