MySQL group-by with comma-seperated list - mysql

I have following data
id category_id bookmark_id
1 1 1
2 2 1
3 3 1
4 4 2
5 5 1
and mysql query is
SELECT * from table_name group by bookmark_id
it returns
id category_id bookmark_id
1 1 1
4 4 2
which is fine
But i want one more colum extra
id category_id category_list bookmark_id
1 1 1,2,3,5 1
4 4 4 2
Any idea how to achieve above result?

select min(category_id) as min_cat,
group_concat(distinct category_id order by category_id) as category_list,
bookmark_id
from your_table
group by bookmark_id

Related

MySql Databe - listing products that have only one category

I have table in database like:
===========================
products_id | categories_id
===========================
1 2
1 1
1 3
1 4
1 5
2 5
2 2
2 3
2 4
3 5
4 5
5 5
===========================
I'm not so good in mysql so i need some help with this query. I want to list all products_id that have only categories_id = 5. That is, even if the categories_id is 5, it should only be listed if no other row exists with that products_id and a different categories_id.
So the output should be : 3 4 5.
Here is one approach using aggregation and filtering with a having clause:
select product_id
from mytable
group by product_id
having min(category_id) = 5 and max(category_id) = min(category_id)

mySQL how to count this item id

I have a table
Table item kit
item_kit_id item_id
=======================
1 1
1 1
1 2
2 2
2 237
how to display result
item_kit_id item_id count
===============================
1 1 2
1 2 1
2 2 1
2 237 1
Thanks
You should use group by with both column its return your expected result
SELECT item_kit_id,item_id,count(*)
FROM kit
GROUP BY item_kit_id,item_id;
For more know about GROUP BY
you can use below query-
select item_kit_id, item_id, count(item_id) as count
from item_kit
group by item_kit_id, item_id;

Show latest ID and company ID based on job ID

Table: service
id companyid jobid
1 1 1
2 1 2
3 1 2
4 2 3
5 3 4
6 3 1
7 4 2
8 5 2
I type the following query:
SELECT *
FROM service
WHERE jobID = 2
ORDER BY companyID desc, ID desc
And I get the below output:
id companyid jobid
8 5 2
7 4 2
3 1 2
2 1 2
But I want my expected output is below:
id companyid jobid
8 5 2
7 4 2
3 1 2
How do I modify the query in order to get my expected output?
You should be able to just apply the max function along with a group by clause:
select max(id) id, companyid, jobid
from service
where jobid = 2
group by companyid, jobid
order by id desc
Given your sample data the result would be:
id companyid jobid
8 5 2
7 4 2
3 1 2
This should work:
select * from service s1
join (select max(id) as id
from service
group by jobid, companyid) s2 on s1.id = s2.id
where s1.jobid = 2--can comment to select all latest jobs

How to count how many times that each pair of item has same order id in mySQL

i want to count that how many times that each pair of SKU have been ordered together(have same orderid)
I have an order table of customers that has 2 column ORDERID and SKUID
for an example
ORDERID SKUID
1 1
1 2
1 3
2 1
2 2
2 4
3 1
3 4
i want a result table as
SKU1 SKU2 COUNT
1 2 2
1 3 1
1 4 2
2 3 1
2 4 1
3 4 0
thank you
You can do this with a self-join and aggregation:
select o1.sku, o2.sku, count(distinct o1.orderid) as numorders
from orders o1 join
orders o2
on o1.orderid = o2.orderid and o1.sku < o2.sku;
If you know that the skus are unique in each order, then you can use count(*) instead of count(distinct).

My sql query sum of highest points for each category

upload table
id category_id
1 1
2 2
3 3
4 1
In Ratings Table
id upload_id points
1 1 5
2 2 3
3 3 2
4 4 2
5 1 5
I want to display all the records from ratings table except id 4 because id 4 is the same category id 1
I excepted result
Uploaded_id 1 has sum of 10 points
Uploaded_id 2 has sum of 3 points
Uploaded_id 3 has sum of 2 points
Please help me.
Thanks In advance
Sasikumar
select upload_id, sum(points)
from rating r,
(select min(id) id from upload group by category_id) a
where a.id = r.upload_id group by upload_id