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

  • Home
  • SEARCH
  • 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 8119307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:46:00+00:00 2026-06-06T04:46:00+00:00

I have 4 tables POST: id POST_TAG: post_id tag_id value TAG: id SEARCH: tag_id

  • 0

I have 4 tables

POST:
id

POST_TAG:
post_id
tag_id
value

TAG:
id

SEARCH:
tag_id
post_tag_value

I need to query for posts who have all tags and values as rows in SEARCH table ( not just one equal value for a tag):

EDIT: Sorry for not providing current query and enough information.

SELECT POST.id FROM POST,POST_TAG, SEARCH
WHERE
      POST.id = POST_TAG.post_id AND
      POST_TAG.tag_id= SEARCH.tag_id AND
      POST_TAG.value = SEARCH.value;

It works if SEARCH table has one row. The problem is, when it has more. There should be less results, but there are actually more( if test with 2 rows, the proper results are the rows that are duplicated; I am lookng for intersection not union)

Added sqlfiddle: http://sqlfiddle.com/#!2/9cfb9/1

The result from the query is ‘1’,’1′,’2′ . It should be only ‘1’, because it has both ‘tags’ while ‘2’ has only one.

  • 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-06T04:46:01+00:00Added an answer on June 6, 2026 at 4:46 am

    working example: http://sqlfiddle.com/#!2/393eb/39

    SELECT pt.post_id
    FROM SEARCH s INNER JOIN post_tag pt ON pt.tag_id = s.tag_id AND pt.value = s.value
    GROUP BY pt.post_id
    HAVING COUNT(*) = (SELECT COUNT(*) FROM SEARCH)
    

    Please note that in your fiddle the post with id 0 should be returned as well since it has both (0,'yes') and (1, 'yes') tuples.

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

Sidebar

Related Questions

If I have 3 tables: posts post_id title body tags tag_id name posts_tags post_id
I have a query where I need to get post + every tag that
I have this tables: Posts (post_is, title and text), Tags (tag_id, tag), Post_tag_nn (id,
I have 2 tables: posts tags Tags table is structured like this: post_id tag
I have three tables: tbl_tag tbl_post tbl_post_tag(post_id, tag_id) tbl_users . Each Post has multiple
I have three tables: Post, Attachment, and Media. Posts have Attachments, and Attachments have
I have the following tables: posts (post_id, content, etc) comments (comment_id, post_id, content, etc)
If I have 3 models... Post, Logo_Category and Logo and 5 tables posts, logos,
I am trying to make simple mysql select query, I have 3 tables post:
I have 4 tables and this query SELECT tag.id, title FROM tag LEFT JOIN

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.