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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:22:58+00:00 2026-06-17T15:22:58+00:00

Here’s my category structure: • Department • Category • Sub-Category Here’s my database: I

  • 0

Here’s my category structure:

• Department
  • Category
    • Sub-Category

Here’s my database:

enter image description here

I want to display 6 random products from a specific department. So for example, when a user goes to view a department page I want all products in that department only. The problem I’m having is I cannot figure out a way to get only the department, for some reason I can only get the products from Category … I need to go one more level.

Here’s my SQL:

SELECT *
FROM products p
LEFT JOIN products_categories pc
ON p.product_id = pc.product_id
LEFT JOIN categories c
ON pc.category_id = c.category_id
WHERE c.category = 119
ORDER BY RAND()
LIMIT 6

119 would be the department id, but my query comes up empty because I cannot go that far down.

EDIT
I created an SQLFiddle to make things easier for everyone … very cool tool 😉

http://sqlfiddle.com/#!2/964ed/3

EDIT I’m thinking I need to explain better

So I have my categories three deep; Department, Category, and Sub-Category. The department has no products tied to it, all the products are tied to the sub-categories. Since my products aren’t tied directly to my department, how the heck can I make a query that will retrieve everything under a department? I’m trying to get everything under Firearms since that has the most products. When I query using Firearms id (119) I get nothing? But when I use one of the Sub-Categories id, I get results? So, I’m guessing I need to do some type of sub select or something, but not sure. Hopefully that clears things up 🙂

EXPECTED RESULTS
Using 119, which is the category_id for firearms, I would like to get the following records:

(143, 131, 'single-shot')
(144, 131, 'semiautomatic')
(145, 131, 'bolt-action')
(146, 131, 'pump & lever action')
(171, 130, 'bolt-action')
(174, 136, 'safe')
(178, 130, 'single-shot')
(179, 130, 'pump & lever action')
(180, 130, 'semiautomatic')
(182, 181, 'shotgun ammunition')
(183, 181, 'rimfire ammunition')
(184, 181, 'centerfire ammunition')
(185, 181, 'handgun ammunition')
(186, 120, 'cooking')
  • 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-17T15:22:59+00:00Added an answer on June 17, 2026 at 3:22 pm

    I found a site that explains exactly what I’m looking for:
    http://code-slim-jim.blogspot.com/2010/06/mysql-find-grandparentsgrandchildren.html

    SQL Fiddle:
    http://sqlfiddle.com/#!2/964ed/97

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

Sidebar

Related Questions

Here's the situation, i want to have a user that can enter time on
Here I am trying to retrieve the response from the server and display it,
Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here's my search array: $aExt = array('png','jpg','gif'); I want to search through the variable:
Here is what I want to do. Use this HTML line and have the
Here's the setup - I have a view that lists products. On that same
Here's the link: www.mchenry.edu/maps/google.asp Why won't the location balloon display correctly? Also, the pop-up
Here's my situation. I have a DotNetNuke application. I want to link to an

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.