Need SQL Queries to list categories with no products and Products not in a Category

Open to all! Ask other shopowners for help.
Post Reply
lecarlb
Contributor
Posts: 316
Joined: Mon Oct 26, 2020 5:26 pm
Phoenix Version:
Has thanked: 48 times
Been thanked: 9 times

Need SQL Queries to list categories with no products and Products not in a Category

Post by lecarlb »

Hello,

I know they're in the old forum but I couldn't find them. Or if anyone here can help me find a way to sift through the products_to_categories table and weed out the products that are redundant or unnecessary.

I'm open to suggestions.

Thank you.


Join The Code Co-op to get access to your library in the Code Co-op Forum
User avatar
burt
Core Team
Posts: 4560
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Need SQL Queries to list categories with no products and Products not in a Category

Post by burt »

List Categories with no products

Code: Select all

SELECT categories_id FROM categories where categories_id NOT IN (select categories_id from products_to_categories);
Be careful here. It is possible that a parent category only contains other categories, so this would show up as a category with no products. If you delete that parent category, it could cause your site a lot of hurt.

List Products not in a category

Code: Select all

SELECT products_id FROM products where products_id NOT IN (select products_id from products_to_categories);
I am not here to build for you.
I am here to build with you. Let's help each other.
lecarlb
Contributor
Posts: 316
Joined: Mon Oct 26, 2020 5:26 pm
Phoenix Version:
Has thanked: 48 times
Been thanked: 9 times

Re: Need SQL Queries to list categories with no products and Products not in a Category

Post by lecarlb »

burt wrote: Thu Jun 29, 2023 4:34 pm List Categories with no products

Code: Select all

SELECT categories_id FROM categories where categories_id NOT IN (select categories_id from products_to_categories);
Be careful here. It is possible that a parent category only contains other categories, so this would show up as a category with no products. If you delete that parent category, it could cause your site a lot of hurt.

List Products not in a category

Code: Select all

SELECT products_id FROM products where products_id NOT IN (select products_id from products_to_categories);
Thank you. Your reply will be great for future reference.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Need SQL Queries to list categories with no products and Products not in a Category

Post by ecartz »

LEFT JOINs would be more efficient than NOT IN.

Code: Select all

SELECT p.products_id FROM products p LEFT JOIN products_to_categories p2c ON p.products_id = p2c.products_id WHERE p2c.categories_id IS NULL

Code: Select all

SELECT c.categories_id FROM categories c LEFT JOIN products_to_categories p2c ON c.categories_id = p2c.categories_id LEFT JOIN categories p ON c.categories_id = p.parent_id WHERE p.categories_id IS NULL AND p2c.products_id IS NULL


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply