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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:42:31+00:00 2026-05-13T06:42:31+00:00

I’m working on a catalog site where users can browse categories. Categories can contain

  • 0

I’m working on a catalog site where users can browse categories. Categories can contain other categories and products, and products can belong to more than one category. The relevant database schema looks something like this:

CREATE TABLE products (
    product_id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
    product_title VARCHAR(100) NOT NULL,
    product_status TINYINT UNSIGNED NOT NULL
);

CREATE TABLE product_categories (
    category_id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
    parent_category_id INT UNSIGNED NOT NULL,
    category_title VARCHAR(100) NOT NULL,
    category_status TINYINT UNSIGNED NOT NULL,
    category_order INT UNSIGNED NOT NULL
);

CREATE TABLE products_categories (
    product_id INT UNSIGNED NOT NULL,
    category_id INT UNSIGNED NOT NULL,
    product_order INT UNSIGNED NOT NULL,
    PRIMARY KEY(product_id, category_id)
);

The issue i’m having is I need to paginate the results using LIMIT n, n:

$perpage = 20;
$start = (isset($_GET['page'])) ? (int)$_GET['page'] * $perpage : 1;
$limitsql = "LIMIT $start, $perpage";

But I can’t figure out how to select both distinct categories and products without joining and merging the results. Ideally I would like results like this:

product_id      |       product_title   |       category_id     |       category_title
NULL            |       NULL            |       32              |       category foo
NULL            |       NULL            |       239             |       category bar
9391            |       product foo     |       NULL            |       NULL
325             |       product bar     |       NULL            |       NULL

The best I’ve been able to do is get something like this, which doesn’t really help:

product_id      |       product_title   |       category_id     |       category_title
9391            |       product foo     |       32              |       category foo
325             |       product bar     |       239             |       category bar
239             |       product foo2    |       32              |       category foo
115             |       product bar2    |       239             |       category bar

The only other solutions that I can think of would be to query all subcategories and products within the category, stick them in a php array and extract the current page with array_slice. Considering the volume of products (several thousand) this isn’t a very appealing option.

Otherwise I could query the number of categories, and offset the $start in the LIMIT clause by the number of categories. This get’s messy though if there is more than a full page of categories.

Here is my current working query which gives me the results above:

SELECT
    p.product_id, p.product_title,
    c.category_id, c.category_title
FROM products AS p
JOIN product_categories AS c
    ON c.parent_category_id='20'
INNER JOIN products_categories AS pc
    ON p.product_id=pc.product_id
WHERE p.product_status='1' AND pc.category_id='20'
ORDER BY pc.product_order ASC

Edit

I think i’ve got it working with UNION, which I completely forgot about

SELECT
    c.category_id AS row_id, c.category_title AS row_title, 1 AS is_category
FROM product_categories AS c
WHERE c.parent_category_id='20'

UNION 

SELECT
    p.product_id AS row_id, p.product_title AS row_title, 0 AS is_category
FROM products AS p
INNER JOIN products_categories AS pc
    ON p.product_id=pc.product_id

Edit 2

I guess Union isn’t going to work as I thought. Since both are treated as separate queries I can’t apply LIMIT to the entire result, only each individual SELECT. Also it seems the columns selected from each statement must be of the same type of the corresponding type in the other statement.

  • 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-13T06:42:31+00:00Added an answer on May 13, 2026 at 6:42 am

    Use:

    SELECT *
      FROM (SELECT c.category_id AS row_id, c.category_title AS row_title, 1 AS is_category
              FROM product_categories AS c
             WHERE c.parent_category_id='20'
            UNION 
            SELECT p.product_id AS row_id, p.product_title AS row_title, 0 AS is_category
              FROM products AS p
              JOIN products_categories AS pc ON p.product_id=pc.product_id) x
    LIMIT x, y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.