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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:24:55+00:00 2026-05-21T11:24:55+00:00

SELECT t.tag_name FROM tags t JOIN resource_tags rt ON rt.tag_id = t.tag_id JOIN resource

  • 0
SELECT t.tag_name
FROM tags t
 JOIN resource_tags rt ON rt.tag_id = t.tag_id
 JOIN resource r ON r.resource_id = rt.resource_id
 JOIN visitor_resource vr ON vr.resource_id = rt.resource_id
 JOIN visitor v ON v.visitor_id = vr.visitor_id
WHERE v.visitor_id = '1'
GROUP BY t.tag_name

As you might see, a ‘visitor’ visits a ‘resource’ and a relation between them will be created in visitor_resource.

A given ‘resource’ have a couple of ‘tags’ depending on the content, and they are bound together by a relation in resource_tags.

The above query outputs the tag-names for all the resources that a visitor have visited.

Now I want to find out how many times a tag is represented.

Consider the following:
Resource 1: tag1, tag2
Resource 2: tag1
Resource 3: tag2
Resource 4: tag1

The query should output:
tag1, 3
tag2, 2

I have tried with the following:

SELECT t.tag_name, SUM(t.tag_id) as cnt
 FROM tags t
 JOIN resource_tags rt ON rt.tag_id = t.tag_id
 JOIN visitor_resource vr ON vr.resource_id = rt.resource_id
 JOIN visitor v ON v.visitor_id = vr.visitor_id
WHERE v.visitor_id = '2'
GROUP BY t.tag_name

But that seems to have exceptionally unreasonable high numbers for cnt, and not counting for this specific user.

Is this even possible with MySQL?

  • 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-05-21T11:24:55+00:00Added an answer on May 21, 2026 at 11:24 am

    Use: COUNT(t.tag_id)

    and not SUM(t.tag_id).

    It’s also more common to group by the id (primary key) and perhaps faster as the index can be used for the grouping. The tag_name may not have an index.

    SELECT t.tag_name
         , COUNT(t.tag_id) AS cnt
    FROM tags t
      JOIN resource_tags rt ON rt.tag_id = t.tag_id
      JOIN visitor_resource vr ON vr.resource_id = rt.resource_id
      JOIN visitor v ON v.visitor_id = vr.visitor_id
    WHERE v.visitor_id = '2'
    GROUP BY t.tag_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( SELECT root_tags.tag_id, root_tags.tag_name, COUNT( root_tagged.pg_id ) FROM root_tags LEFT JOIN root_tagged ON (
SELECT id,name,color FROM animals a INNER JOIN animal_tags atags ON atags.animal_id = a.id WHERE
3 tables: items(item_id, ...) tags(item_id, tag_name) downloads(item_id, ...) How do I select a single
With the following query: SELECT p.*, i.*, GROUP_CONCAT(DISTINCT(pc.category_id)) AS post_categories, GROUP_CONCAT(DISTINCT(pt.tag_id)) AS post_tags FROM
So I need to select distinct values from one table, but join all the
Here is my table in the dataset: SELECT tag_work_field.* FROM tag_work_field I created this
I have a table tags with fields tag_id and name . I have a
I'm trying to join 3 tables tags, itemtags and transactions. Transactions is a list
SELECT `v`.* , GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids` , GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres` , GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
I'm trying to select all the tags that go with a post in my

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.