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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:57:03+00:00 2026-06-13T05:57:03+00:00

I’ve been looking at this for too long and I’ve have lost all capacity

  • 0

I’ve been looking at this for too long and I’ve have lost all capacity for logic. Pointers/help much appreciated.

I have three tables as below, with the columns that matter.

Table: Categories

+-------+--------------+
| catID | catName      |
+-------+--------------+
|     1 | somename     |
|     2 | stringname   |
|     3 | thirdcat     |
|     4 | stringcat    |
|     5 | stringother  |
|     6 | sixthcat     |
+-------+--------------+

Table: Products

+-----+------+
| pID | pCat |
+-----+------+
|  22 |    1 |
|  33 |    1 |
|  44 |    7 |
|  55 |    9 |
+-----+------+

Table: Prodcats

+-----+-------+
| pID | catID |
+-----+-------+
|  22 |     2 |
|  22 |     6 |
|  33 |     3 |
|  33 |     5 |
|  44 |     3 |
|  44 |     6 |
|  55 |     5 |
|  55 |     6 |
+-----+-------+

I have a list of category IDs. I need to use this list to return categories with a particular string in catName.

I want categories added to this list if (from categories):

catName LIKE %string% AND catID IN ($mycats) 

Obviously no problem there.

However I also need to return categories 2 and 5 (containing ‘string’) but ONLY if they are related (in prodcats) 2to a product that is in catID 1.

That’s where my head broke.

It may be useful to know the following:

  • The IDs in $mycats may or may not occur as pCat in the products table.
  • There will only ever be either one or two items in $mycats – if there are two, one will always be a cat whose name contains the string I am looking for, the second one will never be.

EDITED: – added sample table data above and an example below to make this clearer.

An example would be:
$mycats=1,4

I would need to get catID 4 – whose names both contains ‘string’. That’s the easy bit.

BUT I also want to get catID 2 and 5, because they too contains string and although are not in $mycats are attached to a pID in the table prodcats which is in catID 1 in products.

I hope that makes it a bit clearer. It’s like mud to me at this point.

  • 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-13T05:57:04+00:00Added an answer on June 13, 2026 at 5:57 am

    The secondary category ids that are also to be included from your requirement may be expressed as:

    select c.*
    from Product p
    join Prodcats pc on pc.pID = p.pID
    join Categories c on c.catID = pc.catId and c.catName like '%string%'
    where pCat in (1,4)
    

    Now you just union that with your original query:

    select *
    from Categories = pc.catId 
    where catID in (1,4)
    and c.catName like '%string%'
    union
    select c.*
    from Product p
    join Prodcats pc on pc.pID = p.pID
    join Categories c on c.catID = pc.catId and c.catName like '%string%'
    where pCat in (1,4)
    

    Notes:

    • union removes duplicates automatically, so no need for distinct. (union all preserves duplicates)
    • this query should perform quite well – the two paths to categories are in a separate queries, so optimization will be good
    • the condition for category name is in the join’s on clause, because such clauses execute as the join is made, whereas putting the condition in the where cluase would be executed after all possible joins are made, which means a lot more joins are made. Bottom line: It’s much faster this way
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Let's say I'm outputting a post title and in our database, it's Hello Y’all

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.