使用另一个 MySQL 表的 COUNT 更新

发布于 2024-10-05 08:44:45 字数 505 浏览 4 评论 0原文

我正在尝试使用另一个表 Videos 中的 COUNT 来更新表 Profiles 中的列。所有视频都存储在 Videos 表中,我想在 Profiles 中创建一个名为 VideoCount 的列,其中显示该用户视频的 COUNT 个Videos 表。

基本上,我想运行一个 cron 作业 - 所以我需要知道如何在 Videos 表中查找用户视频的计数,然后在 VideoCount 中更新该用户的 VideoCount code>Profiles 包含此计数。

要识别谁上传了哪个视频,Videos.OwnerProfiles.ID 应匹配。有人知道我需要为 PHP 文件 cron 作业运行的 SQL 吗?

I'm trying to update a column in table Profiles with a COUNT from another table Videos. All videos are stored in the Videos table, and I want to create a column in Profiles called VideoCount which shows the COUNT of that user's videos in the Videos table.

Basically, I want to run a cron job - so I need to know how to find the COUNT of a user's videos in the Videos table and then UPDATE that user's VideoCount in Profiles with this count number.

To identify who's uploaded which video, Videos.Owner and Profiles.ID should be made to match. Does anybody know the SQL which I need to run for the PHP file cron job?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

输什么也不输骨气 2024-10-12 08:44:45
UPDATE Profiles
SET VideoCount = (
   SELECT COUNT(*)
   FROM Videos
   WHERE Videos.Owner = Profiles.ID);

尽可能简单^^

,如果您想要它奇特:您可能需要的所有触发器:

DELIMITER //

CREATE TRIGGER increaseVideoCount AFTER INSERT ON Videos
  FOR EACH ROW BEGIN
    UPDATE Profiles SET VideoCount = VideoCount + 1 WHERE ID = NEW.Owner;
  END;
//

CREATE TRIGGER decreaseVideoCount AFTER DELETE ON Videos
  FOR EACH ROW BEGIN
    UPDATE Profiles SET VideoCount = VideoCount - 1 WHERE ID = OLD.Owner;
  END;
//

CREATE TRIGGER checkVideoCount AFTER UPDATE ON Videos
  FOR EACH ROW BEGIN
    IF OLD.Owner <> NEW.Owner THEN
      UPDATE Profiles SET VideoCount = VideoCount + 1 WHERE ID = NEW.Owner;
      UPDATE Profiles SET VideoCount = VideoCount - 1 WHERE ID = OLD.Owner;
    END IF;
  END;
//

DELIMITER ;
UPDATE Profiles
SET VideoCount = (
   SELECT COUNT(*)
   FROM Videos
   WHERE Videos.Owner = Profiles.ID);

as simple as it can be^^

and if you want it fancy: all the triggers you might need:

DELIMITER //

CREATE TRIGGER increaseVideoCount AFTER INSERT ON Videos
  FOR EACH ROW BEGIN
    UPDATE Profiles SET VideoCount = VideoCount + 1 WHERE ID = NEW.Owner;
  END;
//

CREATE TRIGGER decreaseVideoCount AFTER DELETE ON Videos
  FOR EACH ROW BEGIN
    UPDATE Profiles SET VideoCount = VideoCount - 1 WHERE ID = OLD.Owner;
  END;
//

CREATE TRIGGER checkVideoCount AFTER UPDATE ON Videos
  FOR EACH ROW BEGIN
    IF OLD.Owner <> NEW.Owner THEN
      UPDATE Profiles SET VideoCount = VideoCount + 1 WHERE ID = NEW.Owner;
      UPDATE Profiles SET VideoCount = VideoCount - 1 WHERE ID = OLD.Owner;
    END IF;
  END;
//

DELIMITER ;
后来的我们 2024-10-12 08:44:45

如果您想更新视频计数,您可以在上传时更新视频计数,因此我认为不需要 cron 作业。

update profiles set videocount=(select count(*) from videos where profileid =$profileid ) 
where profileid=$profileid

If you want to update video count that you can upadate video count at the time of upload, so as i think there is no need for the cron job for this.

update profiles set videocount=(select count(*) from videos where profileid =$profileid ) 
where profileid=$profileid
一念一轮回 2024-10-12 08:44:45

就像

UPDATE A SET VideoCount = 
(
  SELECT COUNT(B.*) 
  FROM Videos AS B INNER JOIN Profiles AS B ON (A.Profile_ID = B.Owner_ID)  
  GROUP BY B.Owner_ID
);

我不确定这个语法是否完全正确。尝试一下

Something like

UPDATE A SET VideoCount = 
(
  SELECT COUNT(B.*) 
  FROM Videos AS B INNER JOIN Profiles AS B ON (A.Profile_ID = B.Owner_ID)  
  GROUP BY B.Owner_ID
);

I'm not sure this syntax is completely correct. give it a try

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文