I have 3 tables like these:
product
product_id | name
1 | Pizza Margherita
2 | Pizza Salsiccia
3 | Pizza Marinara
filter_description
filter_id | name
1 | Mozzarella
2 | Pomodoro
3 | Salsiccia
product_filter
product_id | filter_id
1 | 1
1 | 2
2 | 1
2 | 2
2 | 3
3 | 2
I need to join 3 tables and make two queries, the first for all pizza without 'Mozzarella', and the second for all pizza with 'Mozzarella' AND 'Salsiccia'.
You could use below query for your criteria
For all pizza without 'Mozzarella'
select p.product_id, p.name
from product p
join product_filter pf using(product_id)
join filter_description fd using(filter_id)
group by p.product_id, p.name
having sum(fd.name = 'Mozzarella') = 0;
For all pizza with 'Mozzarella' AND 'Salsiccia'
select p.product_id, p.name
from product p
join product_filter pf using(product_id)
join filter_description fd using(filter_id)
group by p.product_id, p.name
having sum(fd.name = 'Mozzarella') = 1
and sum(fd.name = 'Salsiccia') = 1;
Demo
Variants with NOT IN and IN
-- all pizza without 'Mozzarella'
SELECT *
FROM product
WHERE product_id NOT IN(
SELECT DISTINCT pf.product_id
FROM product_filter pf
JOIN filter_description fd ON fd.filter_id=pf.filter_id
WHERE fd.name='Mozzarella'
)
-- all pizza with 'Mozzarella' AND 'Salsiccia'.
SELECT *
FROM product
WHERE product_id IN(
SELECT pf.product_id
FROM product_filter pf
JOIN filter_description fd ON fd.filter_id=pf.filter_id
WHERE fd.name IN('Mozzarella','Salsiccia') -- condition 1
GROUP BY pf.product_id
HAVING COUNT(fd.name)=2 -- condition 2
)
Related
So I have table with products
id | product_name
1 | Product 1
2 | Product 2
3 | Product 3
... table with atributtes:
id | attribute
1 | big
2 | orange
3 | expensive
and table with products and their attributes
id | product_id | attribute_id
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
4 | 3 | 2
and what I want is to filter big, orange products.. in this case: Product 1
Something like:
SELECT product_name
FROM products as a
JOIN products_attributes as b ON a.id=b.product_id
WHERE b.attribute_id = 1 OR b.attribute_id=2
will not work as it returns Product 3 as well..
This doesn't work too, of course:
SELECT product_name
FROM products as a
JOIN products_attributes as b ON a.id=b.product_id
WHERE b.attribute_id = 1 AND b.attribute_id=2
Please help :)
You need to add atributtes table into your SELECT statement and
filter by attribute IN ( 'big','orange' )
GROUPing with HAVING clause should be added to satisfy the both conditions at the same time
SELECT p.product_name
FROM products as p
JOIN products_attributes as pa
ON p.id = pa.product_id
JOIN attributes a
ON a.id = pa.attribute_id
WHERE a.attribute IN ( 'big','orange' )
GROUP BY p.product_name
HAVING COUNT(DISTINCT a.attribute) = 2
using IN rather than OR operator is more straightforward to use .
Demo
In order to find products that have multiple attributes, first we need to join the attributes table with itself:
SELECT pa1.product_id FROM products_attributes as pa1
JOIN products_attributes as pa2 ON pa1.product_id = pa2.product_id
AND pa1.attribute_id = 1 AND pa2.attribute_id = 2
This will output 1, which is the correct id. Note that it doesn't matter which of the two tables you will select the product_id from, since you're joining it with itself and both will contain it.
Now all we need is the name, so we're going to join the products table as well and change our selection:
SELECT p.product_name FROM products p
JOIN products_attributes as pa1 ON pa1.product_id = p.id
JOIN products_attributes as pa2 ON pa1.product_id = pa2.product_id
AND pa1.attribute_id = 1 AND pa2.attribute_id = 2
This should output "Product1".
I have following tables:
Table: person
id | name
1 | John
2 | Ana
3 | Thomas
Table: fruit
id | name
1 | orange
2 | banana
3 | grapefruit
4 | lemon
5 | apricot
Table: person_fruit
person_id | fruit_id
1 | 1
1 | 3
2 | 1
2 | 2
1 | 5
As you guessed, the person_fruit table serves as the many to many relation.
I'm doing a query for listing all the persons and their favourite fruits concatenated in a string. My problem is that I cannot display their favourite fruits ordered by fruit name like:
John | apricot, grapefruit, orange
Ana | banana, orange
Thomas | NULL
My current MySQL query looks like:
SELECT
p.name,
GROUP_CONCAT(f.name SEPARATOR ', ') fruit
FROM
person p
LEFT JOIN person_fruit pf
ON p.id = pf.person_id
LEFT JOIN `fruit` `f`
ON f.id = pf.fruit_id
GROUP BY
p.id
how can you sort the concatenated left join results?
You can use ORDER BY inside the GROUP_CONCAT.
Also p.name should be in GROUP BY clause
SELECT
p.name,
GROUP_CONCAT(f.name ORDER by f.name ASC SEPARATOR ',') fruit
FROM
person p
LEFT JOIN person_fruit pf
ON p.id = pf.person_id
LEFT JOIN `fruit` `f`
ON f.id = pf.fruit_id
GROUP BY
p.name
In a MySQL database I have the following tables:
customers
CUSTOMER_ID | NAME
---------------------------
1 | John Doe
2 | Peter Jones
3 | David Smith
products
PRODUCT_ID | DESCRIPTION
---------------------------
1 | Toothbrush
2 | Shaving cream
3 | Deodorant
customer_product
CUSTOMER_ID | PRODUCT_ID
---------------------------
1 | 2
1 | 3
2 | 1
3 | 1
3 | 2
The table customer_product is a pivot table. When a customer orders a product, it will be logged there.
My question is: how can I select all customers that didn't ordered a certain product?
So for example, I want to retrieve all customers that never ordered a Toothbrush.
You can use NOT EXISTS:
SELECT CUSTOMER_ID,NAME
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1
FROM customer_product AS cp
INNER JOIN products AS p
ON cp.PRODUCT_ID= p.PRODUCT_ID
WHERE p.DESCRIPTION = 'Toothbrush' AND cp.CUSTOMER_ID = c.CUSTOMER_ID)
Here is another way that will work for you
select c.customer_id,c.Name from customers c
where c.customer_id not in
(select c.customer_id from customers c
left join customer_product cp on c.customer_id = cp.customer_id
inner join products p on cp.product_id = p.product_id
where p.description ='toothbrush'
) ;
I'm not sure I phrased the question correctly, so feel free to correct me. Here are the tables with their data:
product category category_product
------- -------- ----------------
id_product id_category active id_category id_product
1 1 1 1 1
2 2 1 2 1
3 3 0 1 2
4 0 2 2
3 2
3 3
4 3
I need to select only those products, which have all categories as inactive.
For example:
Product 1 is good, since it belongs to active categories (1, 2).
Product 2 is good, since it has at least one active category (1, 2; 3 - inactive)
Product 3 must be selected, since all its categories are inactive (3, 4).
I have the following query, which is obviously incorrect, since it selects both products: 2 and 3:
SELECT p.id_product
FROM product p
JOIN category_product cp
ON p.id_product = cp.id_product
JOIN category c
ON c.id_category = cp.id_category
WHERE
c.active = 0;
Here is the SQL Fiddle: http://sqlfiddle.com/#!2/909dd/2/0
How can I solve this?
This way you can select product without active category.
SELECT p.id_product
FROM product p
WHERE NOT EXISTS
(SELECT * FROM
category_product cp
INNER JOIN category c ON c.id_category = cp.id_category
WHERE p.id_product = cp.id_product AND c.active = 1);
SQL Fiddle
Consider the following:
SELECT p.*
, COUNT(*)
, SUM(c.active = 1) active
, SUM(c.active = 0) inactive
FROM product p
JOIN category_product cp
ON cp.id_product = p.id_product
JOIN category c
ON c.id_category = cp.id_category
GROUP
BY p.id_product;
+------------+----------+--------+----------+
| id_product | COUNT(*) | active | inactive |
+------------+----------+--------+----------+
| 1 | 2 | 2 | 0 |
| 2 | 3 | 2 | 1 |
| 3 | 2 | 0 | 2 |
+------------+----------+--------+----------+
http://sqlfiddle.com/#!2/909dd/55
The last part of this problem has been left as an exercise for the reader
This is what i get while trying...apologies if anything is wrong
set #count:=0;
select a.id_product,a.times from
(SELECT count(p.id_product)times, p.id_product, c.active,
if(c.active!=0, #count:=#count+1, #count:=0) x
From category_product cp
join product p
on (p.id_product = cp.id_product)
join category c
on(c.id_category = cp.id_category )
group by id_product )a
where a.x=0;
I need to join tables to do aggregation. I suck at that. Here's my scenario:
CATEGORIES
CatID | CategoryName | Parent
1 | Cars | NULL
2 | Electronics | NULL
3 | DVD | 2
4 | Blu_ray | 2
5 | Shoes | NULL
So basically, topmost elements don't have parents. Then I have
PRODUCTS
ProdID | Prod Name | CatID
1 | DVD Player 1 | 3
2 | Blu-Ray Player | 3
3 | Nike | 5
4 | DVD Player 2 | 3
I want to end up with...
CATEGORIES
CatID | CategoryName | Parent | totalProds
1 | Cars | NULL | 0
2 | Electronics | NULL | 0
3 | DVD | 2 | 2
4 | Blu_ray | 2 | 1
5 | Shoes | NULL | 1
Any ideas?
Aren't you just asking for the total number of products per category?
SELECT CatID, CategoryName, Parent, COUNT(*) totalProds
FROM categories c
INNER JOIN products p ON p.CatID = c.CatID
GROUP BY CatId
See below query
SELECT
c.`CatID`,
c.`CategoryName`,
c.`Parent`,
COALESCE(COUNT(DISTINCT p.`ProdID`),0) AS totalProds
FROM `CATEGORIES` c
LEFT JOIN `PRODUCTS` p
ON p.`CatID` = c.`CatID`
ORDER BY c.`CatID`
LEFT JOIN to return a row for every category. COALESCE to just make sure a 0 is returned if appropriate.
Assuming you have a limited number of category levels, you can union each level at a time. Here's for 4 levels:
SELECT c1.CatID, c1.CategoryName, c1.Parent, COUNT(1) totalProds
FROM products p
INNER JOIN categories c1 ON c1.CatID = p.CatID
GROUP BY c1.CatID, c1.CategoryName, c1.Parent
UNION
SELECT c2.CatID, c2.CategoryName, c2.Parent, COUNT(1)
FROM products p
INNER JOIN categories c1 ON c1.CatID = p.CatID
INNER JOIN categories c2 ON c2.CatID = c1.Parent
GROUP BY c2.CatID, c2.CategoryName, c2.Parent
UNION
SELECT c3.CatID, c3.CategoryName, c3.Parent, COUNT(1)
FROM products p
INNER JOIN categories c1 ON c1.CatID = p.CatID
INNER JOIN categories c2 ON c2.CatID = c1.Parent
INNER JOIN categories c3 ON c3.CatID = c2.Parent
GROUP BY c3.CatID, c3.CategoryName, c3.Parent
UNION
SELECT c4.CatID, c4.CategoryName, c4.Parent, COUNT(1)
FROM products p
INNER JOIN categories c1 ON c1.CatID = p.CatID
INNER JOIN categories c2 ON c2.CatID = c1.Parent
INNER JOIN categories c3 ON c3.CatID = c2.Parent
INNER JOIN categories c4 ON c4.CatID = c3.Parent
GROUP BY c4.CatID, c4.CategoryName, c4.Parent
Hope you get the idea...