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
Related
Table structure(representative)
ID NAME PARENT
--------------------
1 cat1 0
2 cat1 1
3 cat2 1
4 cat1 2
5 cat2 2
6 cat3 2
7 cat1 3
8 cat2 3
9 cat3 3
10 cat1 1
FOREIGN TABLE data for foreign_sub_category_count
id_parent name
-----------------------
2 a
2 b
2 c
3 a
3 b
3 c
categories may have sub categories.
SELECT t.name,t.id
FROM TABLE_NAME AS t
WHERE t.parent = SOME_ID
SOME_ID = 1
gives me the name,id of all categories with SOME_ID parent id
what i want is to get a count of all sub categories of each row in above result set besides the name
WHERE t.id is parent of sub categories and get count of categories from another table which has the same t.id as parent
EXPECTED RESULT
t.id t.name sub_category_count foreign_sub_category_count
2 cat1 3 3
3 cat2 3 3
10 cat1 0 0
I suspect that you are looking for a recursive query - available in MySQL 8.0:
with recursive cte as (
select id root_id, id from mytable
union all
select c.root_id, t.id from cte c inner join mytable t on t.parent = c.id
)
select
t.*,
(select count(*) - 1 from cte c where c.root_id = t.id) no_children
from mytable t
This adds one column to your original table, which contains the number of direct and indirect descendants of the current row.
Try this:
select
tab1.id,
tab1.name,
coalesce(tab2.counts,0) as sub_category_count,
coalesce(tab3.counts,0) as foreign_sub_category_count
from
(select id,name,parent from representative) tab1
left join
(select t1.parent tab_id, count(*) as counts from representative t1 inner join representative t2 on t1.parent=t2.id group by t1.parent) tab2
on tab1.id=tab2.tab_id
left join
(select parent_id,count(*) as counts from foreign_table group by parent_id) tab3
on tab1.id=tab3.parent_id
where tab1.parent=1 --SOME_ID
you can change the parent_id in where tab1.parent=1 of you choice
Example on DB-FIDDLE
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
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
);
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;
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;