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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:28:45+00:00 2026-05-20T10:28:45+00:00

Sorry for the vague topic but I’m having a hard time explaining this. What

  • 0

Sorry for the vague topic but I’m having a hard time explaining this. What I’m trying to do is fetching an ID representing a post which can be posted in different categories, and I want my post to belong to all three categories to match the criteria. The table looks like this

id      category_id
1            3
1            4
1            8

What I wanna do is fetch an id that belongs to all 3 of these categories, but since they’re on different rows I can’t use

SELECT id FROM table WHERE category_id = '3' AND category_id = '4' AND category_id = '8';

This will of course return nothing at all, since no row matches that criteria. I’ve also tried with the WHERE IN clause

 SELECT id from table WHERE category_id IN (3, 4, 8);

This returns any post in any of these categories, the post I want returned has to be in all three of these categories.

So the question becomes, is there any good way to look for an id that belongs to all three of these categories, or do I have to use the WHERE IN clause and see if I get 3 rows with the id 1, then I’ll know that it occured three times, therefor belongs to all three categories, seems like a bad solution.

Thanks in advance. I appreciate the help. 🙂

  • 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-20T10:28:45+00:00Added an answer on May 20, 2026 at 10:28 am

    You could use group_concat to get a comma separated string of your category id’s and check if that contains all the categories you’re filtering


    SELECT id, GROUP_CONCAT(t2.category_id) as categories
    FROM table AS t1
    INNER JOIN table AS t2 ON t1.id = t2.id
    WHERE
    FIND_IN_SET(‘3’, categories) IS NOT NULL AND
    FIND_IN_SET(‘4’, categories) IS NOT NULL AND
    FIND_IN_SET(‘8’, categories) IS NOT NULL

    Update

    SELECT t1.id, GROUP_CONCAT(t2.category_id) as `categories`
    FROM `table` AS t1
    INNER JOIN `table` AS t2 ON t1.id = t2.id
    HAVING
    FIND_IN_SET('3', `categories`) IS NOT NULL AND
    FIND_IN_SET('4', `categories`) IS NOT NULL AND
    FIND_IN_SET('8', `categories`) IS NOT NULL
    

    The last query would not have worked, since it is a group function the value cannot be used in the WHERE clause but can be used in the HAVING clause.

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

Sidebar

Related Questions

Sorry for opening this topic again, but thinking about this topic itself has started
Sorry about the vague subject but I couldn't think what to put. Here's my
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
Sorry for another vague example...but I have a single class where I'm starting a
Sorry for the vague title, I didn't know how else to phrase this. Suppose
Sorry the above is a little vague it's rather hard to word. I've got
I'm sorry this question is going to be a bit vague because I'm not
First of all, sorry if this question is a little vague and rambling! I'm
Sorry about the vague question. I'm at a loss for words to describe this

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.