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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:36:11+00:00 2026-05-16T01:36:11+00:00

How do I select posts that contain a specific tag if there is a

  • 0

How do I select posts that contain a specific tag if there is a many-to-many relationship between posts and tags?

The problem I am having is that because of the where tag.name = 'xxx', only that tag is selected. I want to select all posts that have the tag specified, together with all of their tags, e.g.,

Post 1 -> tag1, tag2
Post 2 -> tag1, tag3
Post 3 -> tag2, tag3

Currently what I get is:

Post 1 -> tag2 // missing tag1
Post 3 -> tag2 // missing tag3 
  • 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-16T01:36:12+00:00Added an answer on May 16, 2026 at 1:36 am

    Assuming these tables:

    • Posts: id, author, date, content
    • Tags: id, name
    • PostTags: post_id, tag_id

    The last table is often called a join table and facilitates a many-to-many relationship between Posts and Tags.

    SELECT p.*
    FROM posts p
    JOIN posttags pt ON p.id = pt.post_id
    JOIN tags t ON pt.tag_id = t.id
    WHERE t.name = 'sql'
    

    Basically, think of a many-to-many relationship as two one-to-many relationships, because that’s how they’re implemented in normal RDBMSs. So the above query has a one-to-many join from Posts to PostTags and another from Tags to PostTags.

    The PostTags table I created has a composite primary key, being (post_id, tag_id). That combination will be unique. Many disfavour composite keys so you’ll often see people creating a primary key column:

    • PostTags: id, post_id, tag_id

    Either method is fine. It’s largely a philosophical difference.

    Update: if you want to select all the posts that have a particular tag and all the tags those posts have then:

    SELECT p.*
    FROM posts p
    JOIN posttags pt ON p.id = pt.post_id
    JOIN tags t ON pt.tag_id = t.id
    WHERE p.id IN
      (SELECT post_id
      FROM PostTags pt
      JOIN tags t ON pt.tag_id = t.id
      WHERE t.name = 'xyz')
    

    Another way to do this is:

    SELECT p.*
    FROM posts p
    JOIN posttags pt ON p.id = pt.post_id
    JOIN tags t ON pt.tag_id = t.id
    WHERE EXISTS
      (SELECT post_id
      FROM PostTags pt
      JOIN tags t ON pt.tag_id = t.id
      WHERE t.name = 'xyz'
      AND pt.post_id = p.id)
    

    Which performs better will need to be tested and may vary depending on database vendor and version. A good optimizer (ie Oracle) will probably optimize them to perform the same. Others may not.

    Now this will get you rows back like this:

    Post 1, tag 1
    Post 1, tag 2
    Post 3, tag 2
    Post 3, tag 3
    

    so you’ll need to combine them, preferably in application logic rather than SQL. Some RDBMSs have vendor-specific extensions for this kind of thing, like MySQL’s GROUP_CONCAT() function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to select all posts that contain a specific tag. I'm trying it
I'm trying to select posts using MATCH to find strings that contain the exact
This code didn't works as expected. I want to select all the posts that
I'm having a case of the Mondays... I need to select blog posts based
i have 2 tables for categories & posts the categories table contain id name
I have a doctrine query that returns blog posts and their comments: SELECT b,
I want to be able to narrow my blog posts to posts that contain
I have 2 tables: posts tags Tags table is structured like this: post_id tag
jQuery selector Selecting specific span tags I had this problem with jQuery's selector. It
The user selects a state from a drop-down field, then ajax posts that value

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.