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

The Archive Base Latest Questions

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

With some help from someone here on Stackoverflow i almost got to the point

  • 0

With some help from someone here on Stackoverflow i almost got to the point where the query is perfect. I need some little help adjusting the query to fit my needs, and i hope someone could help me out again.

Here’s the case, i have the following 2 tables :

Tweet

and

Tweet_tags

Every tweet has a tag, and a tweet can have more of the same tags. I want to count how many tweets have the same tag within a time span of week since the tweet was posted.

Here is the query as it is now :

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

The results of this query are :

  | ID |     Tag      | Cnt |
-------------------------------
  | 1  |  Testtag     | 2   |
  | 2  |  Testtag     | 1   |
  | 3  |  tweettag3   | 1   |
  | 4  |  tweettag2   | 1   |

I have the testtag 2 times in my database, so the first result is correct, tweettag3 and tweettag2 are in my database 1 time so thats good as well, but these will probably also show multiple results when i add them again. I tried using DISTINCT on s.tag to get rid of the duplicate result, however this gives me a syntax error.

So how i want it to be is :

  | ID |     Tag      | Cnt |
-------------------------------
  | 1  |  Testtag     | 2   |
  | 2  |  tweettag3   | 1   |
  | 3  |  tweettag2   | 1   |

Could someone please help me out on this? If you need more information please say so!

Thanks !!

Edit :

This is how the tables look:

Tweet
---------------
ID
Message
users_id
Date

Tweet_tags
---------------
id
tag
tweet_id
  • 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:12:11+00:00Added an answer on June 17, 2026 at 5:12 pm

    You can just use select distinct, so the query would be:

    SELECT distinct 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
    

    Just remove the id field from the select.

    If you want an id, then you can use the MySQL feature of hidden columns to have:

    select t.id, s.tag, . . .
    
    group by tag
    order by cnt desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With some kindly help from StackOverflow, I've got Unity Framework to create my chained
I need some help understanding what's happening here. This code is from a models/log.py
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
I need some help. I am calling the login function from another class, //
I`m new to JavaScript and I need some help extracting the ID from URL
It gives me the creeps,i'm done, i need some help here, i reverted multiple
With the help of several people from Stack Overflow I've managed to almost perfect
I need some help here. I have a mysql table called dictionary like this:
I hope someone here can help me see where this error is coming from.
I need some help here. I am using razor view in MVC3. I have

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.