简体   繁体   English

从一张表中选择用户 ID 并在另一张表中更新

[英]Select User id from one table and update in another table

I have two table called tbl_users and tbl_img_status .我有两个名为tbl_userstbl_img_status表。 I have column in tbl_img_status like below我在tbl_img_status有列,如下所示

id, user_id,status_text, scd_time,status,post_time

I am looking for run cron using PHP for publish post on time.我正在寻找使用 PHP 运行 cron 以按时发布帖子。 So I have query like below所以我有如下查询

$results = mysqli_query($mysqli,"UPDATE tbl_img_status SET post_time=NOW(),status=1 WHERE status=0 AND scd_time<NOW()");

Now My issue is I also need to update tbl_users column called total_post.现在我的问题是我还需要更新名为 total_post 的 tbl_users 列。 I want increase 1 in total_post of that user id which we have published with first query.我想在我们第一次查询时发布的那个用户 id 的 total_post 中增加 1。 I am new in PHP and MYSQL so not getting proper idea for do it.我是 PHP 和 MYSQL 的新手,所以没有正确的想法。 Let me know if someone can help me for it.如果有人可以帮助我,请告诉我。

You can use one query to update two tables.您可以使用一个查询来更新两个表。 Try this query if that works.如果可行,请尝试此查询。 I have got a hint from this.我从中得到了一个提示。 MySQL, update multiple tables with one query MySQL,一次查询更新多张表

UPDATE tbl_users, tbl_img_status 
SET tbl_img_status.post_time=NOW(),
    tbl_img_status.status=1,
    tbl_users.total_post = tbl_users.total_post+1
WHERE
    tbl_users.id= tbl_img_status.user_id
    AND tbl_img_status.status=0 AND tbl_img_status.scd_time<NOW()

You can use Triggers for that purpose.您可以为此目的使用触发器。

This would update for a specific user if the status changed from 0 to 1如果状态从 0 更改为 1,这将为特定用户更新

DELIMITER $$

CREATE TRIGGER after_tbl_img_status_update
AFTER UPDATE
ON tbl_img_status  FOR EACH ROW
BEGIN
    IF OLD.status <> NEW.status AND NEW.status = 1 THEN
        UPDATE tbl_users  SET total_post  = total_post +1 WHERE id = NEW.user_id;
    END IF;
END$$

DELIMITER ;

IF you don't want to change the Database, you can use a INNER JOIN with And update both table as one.如果您不想更改数据库,则可以使用 INNER JOIN 并将两个表都更新为一个。

Your php will then look like:您的 php 将如下所示:

$results = mysqli_query($mysqli,"UPDATE tbl_users tu INNER JOIN  tbl_img_status  tis ON tu.id = tis.user_id SET tis.post_time=NOW(), tis.status=1, tu.total_post = tu.total_post+1 WHERE tis.status=0 AND tis.scd_time<NOW();");

暂无
暂无

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

相关问题 从存在ID(从另一表)的一个表中选择 - Select from one table where id (from another table) exists Select 来自一个表的 ID 并使用 while 循环插入到另一个表中 - Select ID from one table and insert in another table with while loop 从一个表中检索用户 ID 并将其插入到另一个表中 - Retrieving user id from one table and inserting it into another table 将user_id从sql中的一个表转换为另一个表 - Translating user_id from one table in sql to another table MySQL SELECT COUNT从一个表和ID从另一个表? - MySQL SELECT COUNT from one table and ID from another? 从一个表中选择结果,并为每一行从另一张表中选择具有相同ID的所有行 - Select results from one table and for each row select all rows that have the same id from another table 从两个表中各选择一个记录。 通过将另一个值与新表匹配来更新一个值 - select one record each from two table. update the one value by matching the another one to a new table 从一个表中选择另一个ID,然后在另一个表中将其替换为与第二个表不同的char - Select from one table where id in another and replace integer with char varying from second table mysql-通过id从一个表中选择一个产品,然后从另一个表中选择其关联的图像 - mysql- select a product from one table by id, then its associated images from another table PHP MySQL从一个表中选择ID,从另一个表中选择信息 - PHP MySQL Select ID from one table and information from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM