how to make a complicated query - mysql

I have store table, product table and a store-product table showing their relationship.
*store table*
store_id name ...
1 store1
2 store2
3 store3
*product table*
product_id name ...
1 product1
2 product2
3 product3
*store-product table*
id store_id product_id
1 1 1
2 1 2
3 2 3
4 3 1
5 3 2
6 3 3
Once products are given, I want to get stores that are selling those products.
i.e: If the given product is 1, then stores 1, 3 should be fetched.
If the given products are 1, 2, 3, then only store 3 should be fetched.

You can use group by and having:
select sp.store_id
from store_product sp
where sp.product_id in (1, 2, 3) -- list of products
group by sp.store_id
having count(*) = 3; -- number of elements in list

Shortened table names, but something like this?
select store.name from pt
join spt on spt.product_id = pt.id
join st on spt.store_id = st.id
where st.product_id = 1

Related

Extrating Product id which is not present in Customer_Order Table

my query select p.idProduct from customer_order c, product p where idProduct not in (select c.product_id from customer_order)
table 1 product
1 cooker 500
2 Frying Pane 600
3 Spoon 110
table 2 customerOrder
Name product id
Sohaib 1
Sohaib 2
Ammar 2
Max 2
want to get item not in Customer order table which is product id
After Executing Query Mentioned Above i get
Product id
2
3
1
3
1
3
1
3
You can use not in or not exists:
select p.*
from products p
where not exists (select 1
from customerOrder co
where co.productid = p.id
);

get count(*) from multiple tables sql

I have three tables.
entry
ID title
1 Entry1
2 Entry2
3 Entry3
4 Entry4
user_likes
ID user_id entry_id
1 1 3
2 3 1
3 9 4
4 2 2
user_bookmarks
ID user_id entry_id
1 6 3
2 4 3
3 2 1
4 2 2
What i want is the sum of likes and bookmarks for each entry.
result
entryID likes bookmarks
1 1 1
2 1 1
3 1 2
4 1 0
Also with total sum of likes and bookmarks of each entry.
result2
entryID likes+bookmarks
1 2
2 2
3 3
4 1
I managed to get likes and bookmark result using this query in seperate tables. I was not able to show them together in a single table.
SELECT entry.id, COUNT(entry.id) AS likes FROM entry
INNER JOIN user_like ON user_like.entry_id = entry.id GROUP BY entry.id ORDER BY likes DESC
You should aggregate before joining:
select e.*, coalesce(l.likes, 0) as likes,
coalesce(b.bookmarks, 0) as bookmarks,
(coalesce(l.likes, 0) + coalesce(b.bookmarks, 0)) as both
from entries e left join
(select entryid, count(*) as likes
from likes l
group by entryid
) l
on l.entryid = e.id left join
(select entryid, count(*) as bookmarks
from bookmarks
group by entryid
) b
on b.entryid = e.id;

Select product with properties by group

Product table:
id
1
2
3
Product properties
product_id prop_id
1 1
1 2
1 3
2 2
2 3
3 1
4 2
5 1
5 3
Props has groups. Ex, prop_id (1, 2) is a group #1, prop_id (3) - group #2.
I need to select product which contains one prop from group #1 and one from group #2.
Like this
select * from product_properties WHERE prop_id IN (1, 2) AND prop_id IN (3)
But it`s doesn't work, how to create similar correct query?
That query must return me products with id 1, 2, 3.
Maybe something like this:
SELECT a.product_id, a.prop_id, b.prop_id
FROM product_properties a, product_properties b
WHERE
a.product_id=b.product_id AND
a.prop_id IN (1, 2) AND
b.prop_id = 3
SELECT product_id
FROM product_properties
GROUP BY product_id
HAVING COUNT(CASE WHEN prop_id IN (1, 2) THEN 1 END) > 0 -- mean have at least one property with 1 or 2
AND COUNT(CASE WHEN prop_id = 3 THEN 1 END) = 1 -- mean has property 3
This assume a product cant have same property more than once.

GROUP_CONCAT with LEFT JOIN condition?

Assume that I have these tables:
categories
id name parent_id
1 Category 1 0
2 Category 2 1
3 Category 3 1
4 Category 4 2
products
id name category_id
1 product name 1 2
2 product name 2 2
3 product name 3 3
4 product name 4 4
promos
id product_ids
1 1,2,3
2 1,4
3 2,3
4 4
I want to get all data like this:
product_id product_name promo_ids
1 product name 1 1,2
2 product name 2 1,3
3 product name 3 1,3
4 product name 4 2,4
This is how I query the database:
SELECT GROUP_CONCAT(pr.id) as promo_ids, p.id as product_id, p.name as product_name,
FROM `products` as p
LEFT JOIN `promos` as pr ON `p`.`id` IN (pr.product_ids)
WHERE p.category_id IN(1,2,3,4)
GROUP BY p.id
But the result is not as I expected.
product_id product_name promo_ids
1 product name 1
2 product name 2
3 product name 3
4 product name 4
What was wrong with my query? I guess the problem is the [promos][product_ids] field but I have no idea to solve this.
Thanks in advance!
Storing comma separated ids is not a good idea see Database Normalization,you cannot simply use IN() to join with your set column for now you need to use FIND_IN_SET to find the values in a comma separated set
SELECT
GROUP_CONCAT(pr.id ORDER BY pr.id) AS promo_ids,
p.id AS product_id,
p.name AS product_name
FROM
`products` AS p
LEFT JOIN `promos` AS pr
ON FIND_IN_SET(`p`.`id`,pr.product_ids)
WHERE p.category_id IN (1, 2, 3, 4)
GROUP BY p.id

SQL Counting sums of rows

I have 2 tables.
Table 1: Fruits
id (int, autoincreasing, primary key)
some other junk
Table 2: Customers
has a 'fruit' column, this column contains an id from the fruit table.
i want to query all the customers, and come up with a list of all the fruit ID's and the number of times they are in use.
So this set up:
Fruits has
id name
1 orange
2 banana
3 apple
Customers has 6 rows like:
id fruit
1 1
2 1
3 2
4 2
5 2
6 3
Trying to write a query that Will give me:
fruit id purchase count
1 2
2 4
3 1
Try this
SELECT f.id as fruit_id, COUNT(1) AS purchase_count
FROM FRUITS f LEFT JOIN CUSTOMERS c
ON F.ID = c.fruit
GROUP BY f.id
Should just be a simple aggregate...
SELECT fruit_id, COUNT(fruit_id) AS purchase_count
FROM customers
GROUP BY fruit_id
ORDER BY fruit_id ASC;
untested
SELECT id, SUM(fruit) as "fruit id", "purchase count"
FROM Customers
GROUP BY id;