I have records like this after applying joins
id name item
1 bil Books
2 mike Table
2 mike chair
3 josh pen
3 josh pencil
4 peter copy
But, I want them to look like this
id name item
1 bil Books
2 mike Table,chair
3 josh pen, pencil
4 peter copy
How to achieve this?
Use group by and group_concat():
select id, name, group_concat(item) items
from mytable
group by id, name
Related
I have 1 table name "companies" with several datas like :
Id
Owner
Company
Job
1
John Doe
Company 1
CEO
1
John Doe
Company 2
CEO
1
John Doe
Company 3
CEO
1
Gab Durand
Company 4
CEO
1
Rob Dujn
Company 5
CTO
1
Alex DoeMorse
Company 6
COO
What I need is to get 1 line by company with a row calculating the number of company own by each person.
This is my desired output :
Id
Owner
Company
Job
Count
1
John Doe
Company 1
CEO
3
1
John Doe
Company 2
CEO
3
1
John Doe
Company 3
CEO
3
1
Gab Durand
Company 4
CEO
1
1
Rob Dujn
Company 5
CTO
1
1
Alex DoeMorse
Company 6
COO
1
What could be the mysql query?
EDIT : DB version 5.6.51
Thanks!
You can add an extra column containing analytic function such as
COUNT(*) OVER (PARTITION BY Id, owner) AS count
if DB version is 8.0
As having a former DB version, prefer using correlated subquery such as
SELECT Id, Owner, company, Job,
(SELECT COUNT(*)
FROM t
WHERE id = tt.id
AND Owner = tt.Owner ) AS count
FROM t AS tt
Demo
Query:
SELECT Owner, Job, count(Company) NoOfCompanies
FROM companies
WHERE Job='CEO'
GROUP BY Owner,Job;
Note: Grouping done on Owner and Job expecting you have other values along with 'CEO'
I have a following data below:
Table:
Name Number
Sam 1
Sam 2
Sam 3
John 4
Lani 5
Mera 6
Now I want as a result like this format below
Result:
Name Number
Sam 1,2,3
John 4
Lani 5
Mera 6
How can I possibly do this? What I must going to use?
You can do this with group_concat():
select name, group_concat(number order by number) as numbers
from t
group by name;
Say I have the following tables,
lesson
id description
-----------------
1 science
2 english
3 maths
lesson_student_rel
lesson_id student_id
----------------------
1 1
1 2
1 3
2 1
3 1
3 3
lesson_tutor_rel
lesson_id tutor_id
--------------------
1 1
2 1
2 2
lesson_assistant_rel
lesson_id assistant_id
------------------------
1 1
1 2
3 2
I would like to count the total number of students, tutors and assistants for each lesson so I get a result like,
lesson_id student_count tutor_count assistant_count
---------------------------------------------------------
1 3 1 2
2 1 2 0
3 2 0 1
I think I've been looking at this too long now and have got completely stuck, I've found a few similar questions but nothing that seems to answer this. Is it possible to do with one query? Any pointers would be good - an answer would be amazing.
Thanks!
I think this does it for you:
SELECT l.id,COUNT(DISTINCT lsr.student_id) student_count,COUNT(DISTINCT ltr.tutor_id) tutor_count
,COUNT(DISTINCT lar.assistant_id) assistant_count
FROM lesson l
LEFT JOIN lesson_student_rel lsr ON lsr.lesson_id=l.id
LEFT JOIN lesson_tutor_rel ltr ON ltr.lesson_id=l.id
LEFT JOIN lesson_assistant_rel lar ON lar.lesson_id=l.id
GROUP BY l.id
How do you order a mysql table by the number of occurrences in a spesific column?
Example table:
ID Name
1 Alfred
2 Alfred
3 Burt
4 Alfred
5 Jill
6 Jill
7 Jill
8 Jill
9 Burt
The sorted table should be like below, since "Jill" is the name occurring most, it should be sorted first, and so on:
ID Name
5 Jill
6 Jill
7 Jill
8 Jill
1 Alfred
2 Alfred
4 Alfred
3 Burt
9 Burt
You have to bring in the information into the query. This is typically done using a join:
select e.*
from example e join
(select name, count(*) as cnt
from example
group by name
) en
on e.name = en.name
order by cnt desc, e.name, e.id;
Note that the order by not only orders by the count. It also orders by the name. If two names have the same count, then it will keep them together.
OK have 2 tables
user_id login_history
1 2011-01-01
1 2011-01-02
1 2011-03-05
1 2011-04-05
1 2011-06-07
2 2011-01-01
2 2011-01-02
3 2011-03-05
3 2011-04-05
3 2011-06-07
user_id user_details
1 Jack
2 Jeff
3 Irin
What kind of query can I use to get a result like
1. Jack 2011-01-01 2011-01-02 2011-03-05
2. Jeff 2011-01-01 2011-01-02
3. Irin 2011-03-05 2011-04-05 2011-06-07
Basically I want latest 3 records from table one and be joint with table 2
The query I used will get me a list of below, which is vertical records
Jack ,2011-01-01
Jack ,2011-01-02
Jack ,2011-03-05
Jeff ,2011-01-01
Jeff ,2011-01-02
Irin ,2011-03-05
Irin ,2011-04-05
Irin ,2011-06-07
Please help
select t2.user_details,
substring_index(group_concat(login_history order by login_history separator ' '),' ',3) as recents
from table_2 as t2
left join table_1 as t1
on t1.user_id = t2.user_id
group by t2.user_id
in your example you list first three records, not the last three. By the way you would have just to add desc to the order clause within group_concat if you need it.