Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:12:34+00:00 2026-06-03T19:12:34+00:00

I have a user feed of image posts. Each user can post single images,

  • 0

I have a user feed of image posts. Each user can post single images, however, he can repeat the action often. Say, upload several images within an hour.

How do I effectively design the database table so when a user posts multiple images (one by one) within one hour — I can easily group those serial posts together, eigher on INSERT or on SELECT?


Don’t suggest multi-upload form. That’s not the case: I’ve just described the task in more common terms 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:12:42+00:00Added an answer on June 3, 2026 at 7:12 pm

    That’s out playground:

    CREATE TABLE `feed`(
      `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
      `tm` INT UNSIGNED NOT NULL COMMENT 'timestamp',
      `user_id` INT UNSIGNED NOT NULL COMMENT 'author id',
      `image` VARCHAR(255) NOT NULL COMMENT 'posted image filename',
      `group` INT UNSIGNED NULL DEFAULT NULL COMMENT 'post group',
      PRIMARY KEY(`id`),
      INDEX(`user_id`),
      INDEX(`tm`,`group`)
      );
    

    We’d like to group together posts that are temporally close.

    First, declare the wanted granularity: the threshold to temporal proximity:

    SET @granularity:=60*60;
    

    Each row forms a group with group ID matching the row id (it can also be a timestamp):

    SELECT `g`.`id` AS `group`
    FROM `feed` `g`;
    

    Each group contains rows that originate from the same user, were posted earlier than the group-former:

    SELECT `g`.`id` AS `group`, `f`.*
    FROM `feed` `g`
        CROSS JOIN `feed` `f`
        ON (`f`.`user_id` = `g`.`user_id` 
            AND `f`.`tm` BETWEEN `g`.`tm`-@granularity AND `g`.`tm`
        )
    

    Each row belongs to multiple groups. For each row, we pick the most ‘broad’ group: it has the biggest rowId

    SELECT MAX(`g`.`id`) AS `group`, `f`.*
    FROM `feed` `g`
        CROSS JOIN `feed` `f`
        ON (`f`.`user_id` = `g`.`user_id` 
            AND `f`.`tm` BETWEEN `g`.`tm`-@granularity AND `g`.`tm`
        )
    GROUP BY `f`.`id`
    

    The most recently updated group always jumps to the top (if you sort by group DESC).
    However, if you’d like the groups be persistent (e.g. so items don’t move from one group to another), use MIN instead of MAX:

    SELECT MIN(`g`.`id`) AS `group`, `f`.*
    FROM `feed` `g`
        CROSS JOIN `feed` `f`
        ON (`f`.`user_id` = `g`.`user_id` 
            AND `f`.`tm` BETWEEN `g`.`tm` AND `g`.`tm`+@granularity
        )
    GROUP BY `f`.`id`
    

    Now, we’re going to update the table’s group column.
    First, MySQL can’t update the same table you’re reading from. Wee need a temporary table.
    Second: we only update the rows whose group column is NULL, or rows posted later than UNIX_TIMESTAMP()-2*@threshold:

    CREATE TEMPORARY TABLE `_feedg`
    SELECT MAX(`g`.`id`) AS `group`, `f`.`id`
    FROM `feed` `g`
        CROSS JOIN `feed` `f`
        ON (`f`.`user_id` = `g`.`user_id` 
            AND `f`.`tm` BETWEEN `g`.`tm`-@granularity AND `g`.`tm`
        )
    WHERE `f`.`group` IS NULL 
        OR `f`.`tm` >= (UNIX_TIMESTAMP()-2*@granularity)
    GROUP BY `f`.`id`;
    

    And update the group column:

    UPDATE `feed` `f` CROSS JOIN `_feedg` `g` USING(`id`)
    SET `f`.`group` = `g`.`group`;
    

    Here’s the SQLFiddle: http://sqlfiddle.com/#!2/be9ce/15

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have user generated podcasts, each with their own RSS feed. Is there a
I have a query that pulls back a user's feed which is essentially all
I have a JSON feed data with lots of user relation in it such
I have user input and use htmlentities() to convert all entities. However, there seems
I have a requirement to grab images from an rss feed and display these
I am trying to post a message to a user's feed (wall). I've already
I want to post some text with image on Facebook wall. I have this
In my web application, users can post text to a feed, and the text
I have an action that states a user has placed an order. Similar to
I have user submitted content that is loaded into c# winform in our office

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.