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 9176817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:11:40+00:00 2026-06-17T17:11:40+00:00

I have an assignment to create a twitter like database. And in this assignment

  • 0

I have an assignment to create a twitter like database. And in this assignment i have to filter out the trending topics. My idea was to count the tweets with a specific tag between the date the tweet was made and 7 days later, and order them by the count.

I have the following 2 tables i am using for this query :

Table Tweet : id , message, users_id, date

Table Tweet_tags : id, tag, tweet_id

Since mysql isn’t my strong point at all im having trouble getting any results from the query.
The query i tried is :

Select
      Count(twitter.tweet_tags.id) As NumberofTweets,
      twitter.tweet_tags.tag
From twitter.tweet 
Inner Join twitter.tweet_tags On twitter.tweet_tags.tweet_id = twitter.tweet.id 
WHERE twitter.tweet_tags.tag between twitter.tweet.date and ADDDATE(twitter.tweet.date, INTERVAL 7 day) 
ORDER BY NumberofTweets

The query works, but gives no results. I just can’t get it to work. Could you guys please help me out on this, or if you have a better way to get the trending topics please let me know!

Thanks alot!

  • 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-17T17:11:41+00:00Added an answer on June 17, 2026 at 5:11 pm

    This is equivalent to your query, with table aliases to make it easier to read, with BETWEEN replaced by two inequality predicates, and the ADDDATE function replaced with equivalent operation…

    SELECT COUNT(s.id) As NumberofTweets
         , s.tag
      FROM twitter.tweet t 
      JOIN twitter.tweet_tags s 
        ON s.tweet_id = t.id
     WHERE s.tag >= t.date 
       AND s.tag <= t.date + INTERVAL 7 DAY
     ORDER
        BY NumberofTweets
    

    Two things pop out at me here…

    First, there is no GROUP BY. To get a count by “tag”, you want at GROUP BY tag.

    Second, you are comparing “tag” to “date”. I don’t know your tables, but that just doesn’t look right. (I expect “date” is a DATETIME or TIMESTAMP, and “tag” is a character string (maybe what my daughter calls a “hash tag”. Or is that tumblr she’s talking about?)


    If I understand your requirement:

    For each tweet, and for each tag associated with that tweet, you want to get a count of the number of other tweets, that have a matching tag, that are made within 7 days after the datetime of the tweet.

    One way to get this result would be to use a correlated subquery. (This is probably the easiest approach to understand, but is probably not the best approach from a performance standpoint).

    SELECT t.id
         , s.tag
         , ( SELECT COUNT(1)
               FROM twitter.tweet_tags r
               JOIN twitter.tweet q
                 ON q.id = r.tweet_id
              WHERE r.tag = s.tag
                AND q.date >= t.date
                AND q.date <= t.date + INTERVAL 7 DAY
           ) AS cnt
      FROM twitter.tweet t 
      JOIN twitter.tweet_tags s 
        ON s.tweet_id = t.id
     ORDER
        BY cnt DESC
    

    Another approach would be to use a join operation:

    SELECT t.id
         , s.tag
         , COUNT(q.id) AS cnt
      FROM twitter.tweet t 
      JOIN twitter.tweet_tags s 
        ON s.tweet_id = t.id
      LEFT
      JOIN twitter.tweet_tags r
        ON r.tag = s.tag
      LEFT
      JOIN twitter.tweet q
        ON q.id = r.tweet_id
           AND q.date >= t.date
           AND q.date <= t.date + INTERVAL 7 DAY
     GROUP
        BY t.id
         , s.tag
     ORDER
        BY cnt DESC
    

    The counts from both of these queries assume that tweet_tags (tweet_id, tag) is unique. If there are any “duplicates”, then including the DISTINCT keyword, i.e. COUNT(DISTINCT q.id) (in place of COUNT(1) and COUNT(q.id) respectively) would get you the count of “related” tweets.

    NOTE: the counts returned will include the original tweet itself.

    NOTE: removing the LEFT keywords from the query above should return an equivalent result, since the tweet/tag (from t/s) is guaranteed to match itself (from r/q), as long as the tag is not null and the tweet date is not null.

    Those queries are going to have problematic performance on large sets. Appropriate covering indexes are going to be needed for acceptable performance:

    ...  ON twitter.tweet_tags (tag, tweet_id)
    
    ...  ON twitter.tweet (date) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database which contains an Assignment table. The table looks like this:
I have a database assignment which I have to create some relational algebra for
This is a practice assignment where I have to create a table space with
I have an assignment to create a print view using HTML & CSS. This
I have an assignment where I am to create two tables within a database.
I have an assignment to create a GUI using MATLAB GUIDE and am having
I have an assignment to create a secure communication between 2 people with a
For a school assignment I have to create a C++ program that will create
I have a small assignment in C. I am trying to create an array
For my second programming assignment in my Java class, we have to create a

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.