SQL 'Not in' query involving three tables - mysql

I have three tables, product, category and product_to_category. Product has the primary key product_id, category category_id and product_to_category p2c_id. Product_to_ category links products to categories in a many-to-many relationship using their respective ID's.
Basically I want to write a query that would select all products from categories that do not exist in the category table. This is due to products being migrated across from another database.
I had something like this but was a little lost.
SELECT *
FROM product AS p
LEFT JOIN product_to_category AS p2c ON p.product_id = p2c.product_id
LEFT JOIN category AS c ON c.category_id
Basically that is as far as I have got. I need to join the category table to the product_to_category table where the product_to_category category_id is not in the category table. I may be completely on the wrong path but am stuck!
Thanks in advance.

Assumption: A product can be part of categories that exist, categories that do not exist, or no categories at all. You have not asked for products that belong to no categories at all, so the first LEFT JOIN from product to procduct_to_category should be an INNER JOIN.
Caveat: I am rusty at mysql so I am using SQL SERVER syntax. I forget if mysql has ON clauses or uses where clauses for JOINs. If ON clause is not supported, change them into WHERE clauses.
There are two common approaches: OUTER JOIN or a NOT IN clause (or a NOT EXISTS clause, which often behaves the same performance-wise as the NOT IN clause.)
OUTER JOIN
select p.*, p2c.category_id
from product p
INNER JOIN product_to_category p2c ON (p.product_id = p2c.product_id)
LEFT JOIN category c ON p2c.category_id = c.category_id
WHERE c.category_id IS NULL
The test for null will find the unmatched records.
NOT IN clause
SELECT p.*, p2c.category_id
FROM product p
INNER JOIN product_to_category p2c ON (p.product_id = p2c.product_id)
WHERE p2c.category_id NOT IN (SELECT c.category_id FROM category c)

If you're looking for products from nonexistent categories, I'd suggest
Select p.*,p2c.category_id
from product p
join product_to_category p2c
on p.product_id=p2c.product_id
left outer join category c
on p2c.category_id=c.category_id
where c.category_id is null

SELECT p.*
FROM product AS p
LEFT JOIN product_to_category AS p2c ON p.product_id = p2c.product_id
WHERE NOT EXISTS (
SELECT 1
FROM category c
WHERE c.category_id = p2c.category_id
)

SELECT *
FROM product AS p
JOIN product_to_category AS p2c ON p.product_id = p2c.product_id
JOIN category AS c ON c.category_id != as.category.
Try this?

Related

SQL : multiple table join for data extraction

I have three tables and i want a join to extract data from database.
So that i can get products with names with their category name.
here are three tables.
categories
contains id, name
category_product
contains category_id , product_id,
products
contains id , name, price.
now products.id is fkey in category_product.product_id AND categories.id is fkey in category_product.category_id
You can try below -
select c.name as category_name,p.name as product_name
from category_product cp
inner join categories c on cp.category_id=c.id
inner join products p on cp.product_id=p.id
use join like below
select p.*,pc.*,c.* from product p join category_product pc on p.id= pc.product_id
join categories c on c.id=pc.category_id
select p.name AS product, c.name as category
from products p
inner join category_product cp on cp.product_id = p.id
inner join categories c on c.id = cp.category_id
select products.name as 'Product Name',
categories.name as 'Category Name',*
from category_product
inner join categories on category_product.category_id = categories.id
inner join on products on category_product.product_id = products.id
order by categories.id

SQL query to get number of products for each category

I have a database for an e-commerce store. I'm trying to know the number of active products for each category.
Code for that:
SELECT c.id_category, COUNT(cp.id_product) AS nproducts
FROM ps_category AS c
LEFT JOIN ps_category_product AS cp ON cp.id_category=c.id_category
LEFT JOIN ps_product AS p ON p.id_product=cp.id_product
WHERE p.active=1
GROUP BY c.id_category
ORDER BY nproducts ASC
However, it is not showing categories with 0 products. What am I missing?
Remove WHERE p.active=1 which will fail for any categories which have no products (causing the query to return no rows for those categories) and move that condition into the ON clause for that LEFT JOIN. That way you will still get a NULL row for those categories, which will allow them to show as having 0 products.
SELECT c.id_category, COUNT(p.id_product) AS nproducts
FROM ps_category AS c
LEFT JOIN ps_category_product AS cp ON cp.id_category=c.id_category
LEFT JOIN ps_product AS p ON p.id_product=cp.id_product AND p.active=1
GROUP BY c.id_category
ORDER BY nproducts ASC
Note that to ensure you only count active products you need to count p.id_product, not cp.id_product.

categories and subcategories and products query not working as intended

I am trying to get all the categories and subcategories and there products using a single query from a categories table which has a parent_id for subcategories and a products table. But I am not getting the exact results.
I am not sure where I am doing it wrong.
My query uses 5 tables which are :
products - this table has a products_id field and other details
products_description - this table holds the products_name and products_id fields
products_to_categories - this table contains the products_id linked to categories_id fields
categories - this table contains the categories_id and its other fields and parent_id field
categories_description - this table contains categories_id and categories_name fields.
Here is my query :
SELECT p.products_id, pd.products_name, cd.categories_id AS main_category_id, cd.categories_name ,c.categories_id AS subcategory
FROM products p
INNER JOIN products_description pd ON pd.products_id=p.products_id
INNER JOIN products_to_categories p2c ON p2c.products_id=p.products_id
INNER JOIN categories c ON p2c.categories_id=c.categories_id
INNER JOIN categories_description cd ON cd.categories_id=c.categories_id
GROUP BY cd.categories_id
I think what you are doing wrong is the last inner join on clause condition where you have written cd.categories_id=c.categories_id.
You should change it to this cd.categories_id=c.parent_id
So your final query will be :
SELECT p.products_id, pd.products_name, cd.categories_id AS main_category_id, cd.categories_name ,c.categories_id AS subcategory
FROM products p
INNER JOIN products_description pd ON pd.products_id=p.products_id
INNER JOIN products_to_categories p2c ON p2c.products_id=p.products_id
INNER JOIN categories c ON p2c.categories_id=c.categories_id
INNER JOIN categories_description cd ON cd.categories_id=c.parent_id
GROUP BY cd.categories_id

Make big query from 3 tables

I have three tables
profiles (id, name, deleted)
categories (id, name, deleted)
profiles_categories (id, profile_id, category_id, , deleted)
How i can select all profiles with name in categories?
I trying something like this, but its not works...
SELECT *
FROM profiles p
JOIN categories c, profiles_categories pc
WHERE p.id = pc.profile_id
AND WHERE pc.id = c.category_id
Thanks
EDIT
SELECT *
FROM profiles p
INNER JOIN profiles_categories pc
ON p.id = pc.profile_id
INNER JOIN categories c
ON pc.id = c.id
its return only for one profile (now only two active profiles, but only first have categories)
You have several issues with your current query.
First, you are mixing join types. You should use ANSI JOIN syntax between all of the tables. Don't mix ANSI JOIN syntax with some tables and then commas between other tables.
Second, you have two WHERE clauses and you can only have one WHERE clause.
Finally, you should include the column names that you want to return instead of SELECT *
The query should be similar to this:
SELECT p.name, c.name
FROM profiles p
INNER JOIN profiles_categories pc
ON p.id = pc.profile_id
INNER JOIN categories c
ON pc.id = c.category_id
An INNER JOIN between the tables will return all rows that exist in all of the tables.
Note, based on your table structure you might be able to use the following which returns the profiles that have a corresponding row in the the profiles_categories table:
select p.name
from profiles p
where p.id in (select profile_id
from profiles_categories);
Edit you want to return all profiles regardless of whether or not then have a category, then you need to use a LEFT JOIN:
SELECT p.name, c.name
FROM profiles p
LEFT JOIN profiles_categories pc
ON p.id = pc.profile_id
LEFT JOIN categories c
ON pc.id = c.category_id
SELECT *
FROM profiles p
JOIN profiles_categories pc on p.id = pc.profile_id
JOIN categories c on pc.id = c.category_id
or
SELECT *
FROM profiles p, categories c, profiles_categories pc
WHERE p.id = pc.profile_id
AND pc.id = c.category_id
try this:
SELECT *
FROM profiles p
JOIN profiles_categories pr on p.id = pr.profile_id
JOIN categories c on pr.id = c.category_id
WHERE c.name='thename'

MySQL Join Question

Hi i'm struggling to write a particular MySQL Join Query.
I have a table containing product data, each product can belong to multiple categories. This m:m relationship is satisfied using a link table.
For this particular query I wish to retrieve all products belonging to a given category, but with each product record, I also want to return the other categories that product belongs to.
Ideally I would like to achieve this using an Inner Join on the categories table, rather than performing an additional query for each product record, which would be quite inefficient.
My simplifed schema is designed roughly as follows:
products table:
product_id, name, title, description, is_active, date_added, publish_date, etc....
categories table:
category_id, name, title, description, etc...
product_category table:
product_id, category_id
I have written the following query, which allows me to retrieve all the products belonging to the specified category_id. However, i'm really struggling to work out how to retrieve the other categories a product belongs to.
SELECT p.product_id, p.name, p.title, p.description
FROM prod_products AS p
LEFT JOIN prod_product_category AS pc
ON pc.product_id = p.product_id
WHERE pc.category_id = $category_id
AND UNIX_TIMESTAMP(p.publish_date) < UNIX_TIMESTAMP()
AND p.is_active = 1
ORDER BY p.name ASC
I'd be happy just retrieving the category id's releated to each returned product row, as I will have all category data stored in an object, and my application code can take care of the rest.
Many thanks,
Richard
SELECT p.product_id, p.name, p.title, p.description,
GROUP_CONCAT(otherc.category_id) AS other_categories
FROM prod_products AS p
JOIN prod_product_category AS pc
ON pc.product_id = p.product_id
LEFT JOIN prod_product_category AS otherc
ON otherc.product_id = p.product_id AND otherc.category_id != pc.category_id
WHERE pc.category_id = $category_id
AND UNIX_TIMESTAMP(p.publish_date) < UNIX_TIMESTAMP()
AND p.is_active = 1
GROUP BY p.product_id
ORDER BY p.name ASC
You would use an inner join to the product_category table, doing a left join there is pointless as you are using the value from it in the condition. Then you do a left join on the product_category table to get the other categories, and join in the categories for the data:
select
p.product_id, p.name, p.title, p.description,
c.category_id, c.name, c.title
from
prod_products p
inner join prod_product_category pc on pc.product_id = p.product_id
left join prod_product_category pc2 on pc2.product_id = p.product_id
left join prod_categories c on c.category_id = pc2.category_id
where
pc.category_id = #category_id and
unix_timestamp(p.publish_date) < unix_timestamp() and
p.is_active = 1
order by
p.name