[英]Select User id from one table and update in another table
I have two table called tbl_users
and tbl_img_status
.我有两个名为
tbl_users
和tbl_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.