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

  • SEARCH
  • Home
  • 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 8913789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:32:47+00:00 2026-06-15T04:32:47+00:00

This is a bit challenging but fun question. Consider having these tables tweets tweet_id

  • 0

This is a bit challenging but fun question.
Consider having these tables

tweets

tweet_id | retweet_of_id | user_id

follow

user_id | followed_user_id

So we store each “retweet as a separate tweet” pointing to the original tweet’s id (retweet_of_id). This is because I want to have comments under each one separately.
If something is not a retweet then retweet_of_id will be 0.

How do I retrieve the following using MySQL efficiently?

  • My own tweets
  • All the original tweets (from users that I follow)
  • And the first retweet (by a user I follow) of a tweet (from a user that I don’t follow)

And that the result should a combination of both (in order) just like how twitter does it.
Please consider that there may be 1,000,000 tweets and we only need the most recent ones (e.g.: 10).


Here is an example (I’m user 1 and I follow user 2 & 3)

tweet_id | retweet_of_id | user_id
----------------------------------
    1            0            4          <- EXCLUDE (I don't follow user 4)
    2            0            2          <- INCLUDE (I follow user 2)
    3            0            3          <- INCLUDE (I follow user 3)
    4            1            2          <- INCLUDE (I follow user 2 & first RT)
    5            1            3          <- EXCLUDE (I already have the first RT)
    6            2            3          <- EXCLUDE (I already have the orignal)
    7            0            1          <- INCLUDE (My own tweet)

So the final order should be these tweets: 7, 4, 3, 2 (starting with the most recent)

  • 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-15T04:32:48+00:00Added an answer on June 15, 2026 at 4:32 am

    Here’s how I solved it
    (both of these assume that the tweets are ordered by their tweet_id ASC)

    Solution 1 (correct, runs fast)

    SELECT tweet_id,
    FROM tweets 
    WHERE user = 1 OR user IN (2,3)  
    GROUP BY  IF(retweet_of_id = 0, tweet_id, retweet_of_id)
    ORDER BY tweet_id DESC
    

    Solution 2 (gives correct results, but it’s dog slow for 1,000,000 tweets)

    SELECT p1.tweet_id FROM tweets p1 
    LEFT JOIN tweets p2 
           ON p2.user IN (2,3)
          AND p1.tweet_id > p2.tweet_id
          AND (p1.retweet_of_id = p2.tweet_id 
               OR p1.retweet_of_id AND p1.retweet_of_id = p2.retweet_of_id )
    WHERE p2.tweet_id IS NULL
      AND (p1.user = 1 OR p1.user IN (2,3)) 
    ORDER BY p1.tweet_id DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this bit of obfuscated code. The intention is to create a new object
This is a variation on a previous question as I am having tons of
I tried to research the answer to this question but I'm lost. I am
Coming up with the correct title for this was a bit challenging. So I've
This is a bit of a poser, and I hope you'll find this challenging
Consider this bit of HTML & CSS: <html> <head> <title>Relative Positioning Resize Demo</title> <style>
Okay consider this bit of code: var d1 = new Date(); var d2 =
This is a bit challenging even probably for a team project, let alone for
This bit of code is taking almost a half second to execute. Could somebody
This bit of code comes with new classes that are subclasses of UITableViewController... -

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.