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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:48:01+00:00 2026-06-16T01:48:01+00:00

I have this database schema. I’m calling the stuff I’m tagging objects just for

  • 0

I have this database schema. I’m calling the stuff I’m tagging objects just for the sake of making it simple:

Users

  • user_id
  • login

User to Object table (many to many)

  • user_id
  • object_id

Object table

  • object_id
  • object_stuff

Object to tag with user_id table (many to many with extra column)

  • user_id
  • object_id
  • tag_id

Tag table

  • tag_id
  • tag_name

I have been using a query like this in order to get the objects of a particular user as well as the objects’ tags (if the user tagged any). If you think you can optimize this, feel free to let me know.

@user_id = 'whatever user_id I want';
SELECT 
    o.object_id AS object_id,
    o.object_stuff AS object_stuff,
    IF (tags.tag_name IS NOT NULL, GROUP_CONCAT(tags.tag_name SEPARATOR '|'), 'N/A') AS tags
FROM  object_table AS o
LEFT OUTER JOIN  obj_to_users_table AS o2u ON o.object_id = o2u.object_id
LEFT OUTER JOIN  users AS u ON u.user_id = o2u.user_id 
LEFT OUTER JOIN  obj_to_tag_users AS o2tu ON o.object_id = o2tu.object_id AND u.user_id = o2tu.user_id
LEFT OUTER JOIN  tags AS t ON t.tag_id = o2t.tag_id 
WHERE u.user_id = @user_id GROUP BY o.object_id

Which would yield results like this:

object_id     object_stuff        tags
1             stuff1...           tag1|tag2|tag3
2             stuff2...           tag4|tag6|tag1
3             stuff3...           tag7|tag2|tag5

My problem is that I would like to, for example, search for objects that are tagged as ‘tag2’ and I should get:

object_id     object_stuff        tags
1             stuff1...           tag1|tag2|tag3
3             stuff3...           tag4|tag2|tag1

But instead, I lose the other tags that accompany the object:

object_id     object_stuff        tags
1             stuff1...           tag2
3             stuff3...           tag2

How do I modify my WHERE clause so that I can get the objects tagged as ‘tag2’ but also keeping the other tags the object has in the result set?

... WHERE u.user_id = @user_id AND t.tag_name LIKE '%tag2%' ...
  • 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-16T01:48:02+00:00Added an answer on June 16, 2026 at 1:48 am

    First, calculate the concatenated results.

    Next, perform your search against those concatenated results to preserve the groupings of the tags:

    SELECT * 
    FROM
    (
        SELECT 
            o.object_id AS object_id,
            o.object_stuff AS object_stuff,
            IF (tags.tag_name IS NOT NULL, 
                GROUP_CONCAT(tags.tag_name SEPARATOR '|'), 'N/A') AS tags
        FROM  object_table AS o
        LEFT OUTER JOIN  obj_to_users_table AS o2u 
            ON o.object_id = o2u.object_id
        LEFT OUTER JOIN  users AS u 
            ON u.user_id = o2u.user_id 
        LEFT OUTER JOIN  obj_to_tag_users AS o2tu 
            ON o.object_id = o2tu.object_id AND u.user_id = o2tu.user_id
        LEFT OUTER JOIN  tags AS t ON t.tag_id = o2t.tag_id 
        WHERE u.user_id = @user_id 
        GROUP BY o.object_id
    ) sub
    WHERE sub.tags LIKE '%tag2%'
    

    EDIT:


    Here’s an example that uses an EXISTS. I also switched the LEFT JOINs to INNER JOINs, since searching for a specific tag should eliminate any objects without tags anyways.

    While this query looks more complicated, it should have a better chance of using any available indexes.

    I had also mentioned doing the same type of query using an additional set of INNER JOINs, but that one would look even more complex since you’d still have to use a sub-query to use DISTINCT to get rid any possible duplicates introduced by the additional JOINs… so I’d suggest using EXISTS instead…

    SELECT 
        o.object_id AS object_id,
        o.object_stuff AS object_stuff,
        GROUP_CONCAT(t.tag_name SEPARATOR '|') AS tags
    FROM  object_table AS o
    INNER JOIN  obj_to_users_table AS o2u 
        ON o.object_id = o2u.object_id
    INNER JOIN  users AS u 
        ON u.user_id = o2u.user_id 
    INNER JOIN  obj_to_tag_users AS o2tu 
        ON o.object_id = o2tu.object_id AND u.user_id = o2tu.user_id
    INNER JOIN tags AS t 
        ON t.tag_id = o2t.tag_id
    WHERE 
        u.user_id = @user_id 
        AND EXISTS (
            SELECT 1
            FROM tags AS targetTag
            WHERE 
                targetTag.tag_id = o2t.tag_id
                AND targetTag.tag_name = 'tag2'
        )
    GROUP BY o.object_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a database schema like this: Users UserId RoleUserXRef RoleUserId RoleId UserId
EF 4.1 Database First approach. Say I have this table schema Users 1---M UserRoles
I have an Oracle account (schema) of a remote Oracle database. By using this
So I have this game database, where I have several users with fields, id,
I have this line: @users = database['users'].find(:all).limit(10) it returns this object: <Mongo::Cursor:0x8759a858 namespace='app-development.users' @selector=:all
This is the database schema we have. t_RoleCombinations - These are all possible combination
All, I have to create a single database which has a basic schema. This
Database schema I have this fields: title (string) subtitle description (string) Is better set
I have a database schema like this picture: I want to write a query
I have a database schema like this: My database schema: https://i.stack.imgur.com/vFKRk.png To explain the

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.