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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:34:23+00:00 2026-06-03T16:34:23+00:00

I simplified a many-to-many relationship case with these mockup tables. Posts: —————————— | id

  • 0

I simplified a many-to-many relationship case
with these mockup tables.

Posts:
------------------------------
|   id |    title |     body |
------------------------------
|    1 |      One |    text1 |
|    2 |      Two |    text2 |
|    3 |    Three |    text3 |
------------------------------

Tags:
-------------------
|   id |     name |
-------------------
|    1 |      SQL |
|    2 |     GLSL |
|    3 |      PHP |
-------------------

Post_tags:
------------------------------
|   id |    p_id |      t_id |
------------------------------
|    1 |       1 |         1 |
|    2 |       1 |         3 |
|    3 |       2 |         1 |
|    3 |       3 |         2 |
------------------------------

My goal is to query POSTS with specific TAGS, which I have no problem with, but I also want to display all related tags to the post not just the one I queried for.
My query looks like this:

SELECT p.Title, p.Body, t.name
FROM Posts p
LEFT JOIN Post_tags pt ON p.id = pt.p_id
LEFT JOIN Tags t ON t.id = pt.t_id
WHERE t.name LIKE '%SQL%'

This gets the posts with the “SQL” tag, but it only joins the posts table with tags where it found the “SQL” string, so the other tag “PHP” associated with the post doesn’t get joined.

Obviously the problem is I’m joining the table on the WHERE clause, but how would one solve this in one query or (preferably with subqueries)?

Currently I’m doing this in two separate queries in my application, one for selecting matching posts and another one that is retreiving full post data. This isn’t so efficient and also seems like a lame solution, and I haven’t found a better yet, so I decided to ask the StackOverflow community.

  • 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-03T16:34:24+00:00Added an answer on June 3, 2026 at 4:34 pm

    My old answer is not the shortest, here’s the shortest one:

    select p.*, '' as x, t.name, t.name like '%SQL%'
    from Posts p
    join Posts_tags pt on pt.p_id = p.id 
    join Tags t on t.id = pt.t_id;
    

    Output:

    ID  TITLE   BODY    X       NAME    T.NAME LIKE '%SQL%'
    1   One     text1           SQL     1
    1   One     text1           PHP     0
    2   Two     text2           SQL     1
    3   Three   text3           GLSL    0
    

    So if we group by ID, and check that if at least one (aided by bit_or; Postgresql has this too, aptly named bool_or) of the elements in the group satisfied the ‘%SQL%’ criteria, its bit is ON (aka boolean = true). We can pick that group and we retain all the tags under that group, example, tag id 1 appear on post 1, and post 1 has other tags, which is #3 or PHP. All tags that belong to same Post ID will not be discarded, as we will not be using WHERE filter, we will be using HAVING filter instead:

    select p.*, group_concat(t.name) as tags
    from Posts p
    join Posts_tags pt on pt.p_id = p.id 
    join Tags t on t.id = pt.t_id
    group by p.id
    having bit_or(t.name like '%SQL%');
    

    We can also rewrite that to this:

    select p.*, group_concat(t.name) as tags
    from Posts p
    join Posts_tags pt on pt.p_id = p.id 
    join Tags t on t.id = pt.t_id
    group by p.id
    having sum(t.name like '%SQL%') >= 1;
    

    BIT_OR is like IN, or ANY, so it’s more semantic than evaluating things by SUM

    Output:

    D   TITLE   BODY    TAGS
    1   One     text1   PHP,SQL
    2   Two     text2   SQL
    

    Live test: http://www.sqlfiddle.com/#!2/52b3b/26


    I’m learning so much on stackoverflow. After my old answer, I’m thinking how to make an equivalent shorter code in Postgresql using windowing function(which MySQL don’t have) via SUM OVER partition. Then I thought of Postgresql’s bool_or,bool_and and every function. Then I remember MySQL has bit_or 🙂

    The last solution using SUM is just an afterthought, when I thought up that bit_or is just a semantic of at least one is true, then it’s obvious that you can use HAVING SUM(condition) >= 1 too. Now it works on all database 🙂

    I ended up not solving it by windowing function, the solution above now works on all database 🙂

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

Sidebar

Related Questions

I have two tables: invoice and charges, with a one-to-many relationship. (simplified) invoice has:
I have two tables with a many-to-one relationship. (Oracle) **Table: PARENT** Field: A (PK)
I've mapped two entities using JPA (specifically Hibernate). Those entities have a one-to-many relationship
I've got the following (simplified) problem: I have a entity in a Many-To-One relationship:
Simplified example <td>caption</a> <a id=tt-1>text1</a> <a id=tt-2>text2</a> <td>topics</td> <a id=tt-3>text3</a> <a id=tt-4>text4</a> <a id=tt-5>text5</a>
General Case How do you perform a left join across a many-to-many relationship when
My Core Data model has two entities: Author and Book with a To-Many relationship
I have a many-to-one relationship between objects Product and Supplier . I need to
In my database, Concessions have a many-to-one relationship with Firms (each Concession has a
Here is a simplified version of my database model. I have two tables: Image,

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.