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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:01:06+00:00 2026-06-12T17:01:06+00:00

So I have a query that is trying to grab related posts. Categories have

  • 0

So I have a query that is trying to grab “related posts”.

Categories have a one-to-many relationship with posts. Tags have a many-to-many relationship. So my tables look roughly like this:

posts table:
id | category_id | ... | ...

tags table:
id | ... | ...

post_tag intermediate table:
id | post_id | tag_id | ... | ...

So if I have a single Post row already, and what to grab its “related” posts. My logic is roughly that I want to grab only posts that are in the same category, but to order those posts by the amount of tags that match the original post. So another post in the same category that has the exact same tags as the original post, should be a very high match, whereas a post that only matches 3/4 of the tags will show up lower in the results.

Here is what I have so far:

SELECT *
FROM posts AS p
WHERE p.category_id=?
ORDER BY ( SELECT COUNT(id) 
           FROM post_tag AS i 
           WHERE i.tag_id IN( ? )
         )
LIMIT 5

BINDINGS:
Initial Posts Category ID;
Initial Posts Tag IDs;

Clearly this is not going to actually order the results by the correct values in the sub-select. I am having trouble trying to think of how to join this to achieve the correct results.

Thanks in advance!

  • 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-12T17:01:07+00:00Added an answer on June 12, 2026 at 5:01 pm

    If I undestood your question correctly this is what you’re looking for:

    SELECT p.*, 
           Count(pt.tag_id) AS ord 
    FROM   posts AS currentpost 
           JOIN posts AS p 
             ON p.category_id = currentpost.category_id 
                AND p.id != currentpost.id 
           JOIN post_tag AS pt 
             ON pt.post_id = p.id 
                AND pt.tag_id IN (SELECT tag_id 
                                  FROM   post_tag 
                                  WHERE  post_id = currentpost.id) 
    WHERE  currentpost.id = ? 
    GROUP  BY p.id 
    ORDER  BY ord DESC 
    

    BINDINGS: Initial posts.id;

    and you only have to specify the id of the current post in my version so you don’t have to fetch the posts tags beforehand and format them suitably for an in clause

    EDIT:
    This should be a faster query by avoiding double joining posts, if you don’t like user variables just replace all currentpostid with ? and triple-bind post_id:

    set @currentpostid = ?;
    select p.*, count(pt.tag_id) as ord
    from posts as p, 
    join post_tag as pt
        on pt.post_id = p.id
        and pt.tag_id in (select tag_id from post_tag where post_id = @currentpostid)
    where p.category_id = (select category_id from posts where id=@currentpostid)
        and p.id != @currentpostid
    group by p.id
    order by ord desc;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL query that is baffling me. I am trying to grab
I have an XPath query that's trying to grab the parent of a specific
I have a query that I'm trying to figure the django way of doing
I have a SQL query that I'm trying to debug. It works fine for
I have a SQL query that I'm trying to write, but I'm not quite
I have a database on an external server that I am trying to query.
I have a query that basically combines tables of actions and selects from them
I am trying to write a query that grabs information from one database and
I have two tables that look like this: node: NID Name 1 Chicago 2
I have this mysql query that I am trying to analyze. It is very

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.