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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:27:13+00:00 2026-05-27T06:27:13+00:00

I have two tables, one for posts, one for tags related to posts, where

  • 0

I have two tables, one for posts, one for tags related to posts, where posts.tags holds the ids of the tags related to the post.

posts:

id | title  | tags
-----------------
 1 | Foobar | 1 3 7
 2 | Barfoo | 2 3 7

tags:

id | tag
--------
 1 | Tag1
 2 | Tag2
... and so forth

When querying for a post, I would like to get a post with all its tags as space-seperated string, instead of the string of tag ids.

I tried a query like:

SELECT *, 
(
  SELECT GROUP_CONCAT(tags.tag)
  FROM tags 
  WHERE tags.id LIKE 1 OR tags.id LIKE 3
) AS tag_str
FROM posts

This basically outputs what I am looking for; What I can’t figure out is the correct WHERE condition in the subclause, that would check, in my own words, “for tags WHERE tag.id matches any space-seperated number-string in posts.tags” – if any such query is possible?

Any help appreciated,
k.

  • 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-27T06:27:13+00:00Added an answer on May 27, 2026 at 6:27 am

    Your fundamental database design is flawed and now you’re suffering the consequences of that flaw. If you have any possibility of fixing this, you should do so. The tags should not be stored as a space-delimited string in a single column of the Posts table. Instead, you should have a junction table, Posts_Tags_xref, that would resolve the many-to-many relationship. This table would consist of two foreign keys, Posts.id and Tags.id.

    enter image description here

    For your sample data above, you’d have:

    Posts_Tags_xref
    ==================
    post_id     tag_id
    -------     ------
          1          1
          1          3
          1          7
          2          2
          2          3
          2          7
    

    And the query you’re asking for becomes:

    SELECT p.post_id, p.title, GROUP_CONCAT(t.tag)
        FROM Posts p
            LEFT JOIN Posts_Tags_xref ptx
                INNER JOIN Tags t
                    ON ptx.tag_id = t.tag_id
                ON p.post_id = ptx.post_id
        GROUP BY p.post_id, p.title;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables POSTS and COMMENTS. One post can have many comments and
I have two tables related: DataEntered and Model DataEntered -currentModel Model One DataEntered can
I have two tables Posts and comments Post Table Post_id Post_content Comments table comment_id
I have two tables. One table contains comments on posts, another contains commenter information
Suppose I have two tables, one with blog posts and another with readers and
I have two tables one has a three column composite key. The other needs
I have two tables, one that contains volunteers, and one that contains venues. Volunteers
I have two tables, one stores the products and quantity we have bought, the
I have two tables, one for routes and one for airports. Routes contains just
I have two tables, one called cart and one called items. Now I want

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.