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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:23:33+00:00 2026-06-11T10:23:33+00:00

I have a post/tag database, with the usual post, tag, and tag_post tables. The

  • 0

I have a post/tag database, with the usual post, tag, and tag_post tables. The tag_post table contains tagid and postid fields.

I need to query posts. When I want to fetch posts that have a certain tag, I have to use a join:

... INNER JOIN tag_post ON post.id = tag_post.postid 
WHERE tag_post.tagid = {required_tagid}`

When I want to fetch posts that have tagIdA and tagIdB, I have to use two joins (which I kind of came to terms with eventually).

Now, I need to query posts that do not have a certain tag. Without much thought, I just changed the = to !=:

... INNER JOIN tag_post ON post.id = tag_post.postid 
WHERE tag_post.tagid != {certain_tagid}`

Boom! Wrong logic!

I did come up with this – just writing the logic here:

... INNER JOIN tag_post ON post.id = tag_post.postid 
WHERE tag_post.postid NOT IN 
(SELECT postid from tag_post where tagid = {certain_tagid})

I know this will work, but due to the way I’ve been brought up, I feel guilty (justified or not) whenever I write a query with a subquery.

Suggest a better way to do this?

  • 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-11T10:23:34+00:00Added an answer on June 11, 2026 at 10:23 am

    You can think of it as “find all rows in posts that do not have a match in tags (for a specific tag)”

    This is the textbook use case for a LEFT JOIN.

    LEFT JOIN tag_post ON post.id = tag_post.postid AND tag_post.tagid = {required_tagid}
    WHERE tag_post.tag_id IS NULL
    

    Note that you have to have the tag id in the ON clause of the join.

    For a reference on join types, see here: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

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

Sidebar

Related Questions

I have a page which contains some search fields to query a database, and
I have 4 tables POST: id POST_TAG: post_id tag_id value TAG: id SEARCH: tag_id
Let's say I have a simple database with tables 'posts' and 'tags'. Posts can
I have a MySQL tag-mapping table with structure from the following post - Recommended
I have two tables in my database articles With the following fields id author
Suppose I have a database which contains blog posts which have tags and I
I have the following relationship: Post hasMany Comment Post belongsTo Category Post HABTM Tag
With reference to this post: How to implement tag counting I have implemented the
What I have : A C# database query that returns two columns, one of
Suppose I have following table in my database: Tbl_Persons: Id Country Name 1 Australia

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.