How to group this table column in MySQL? - mysql

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;

Related

How to merge same Ids data with mysql?

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

How to change record in table with consecutive value?

I have table like below :
Application_Number Id_Number1 Name
1 123 John
2 456 Alan
3 789 Charlie
4 111 Patrick
5 222 Robert
Then i would like to update record in one of rows become like this :
Application_Number Id_Number1 Name
1 123 Alias 1
2 456 Alias 2
3 789 Alias 3
4 111 Alias 4
5 222 Alias 5
if i have more than one million record do i need update syntax or any another way? I'm using SQL2008
Thanks
Select Application_Number
,Id_Number1
,'Alias' + CAST( ROW_NUMBER()
OVER (ORDER BY Application_Number) AS Varchar) AS Name
FROM Table Name

Select specified items from rows at first priority mySql

I have a table users
id | Name |
1 John
2 Dwayne
3 Daniel
4 Ronaldo
5 Messi
6 Gareth
7 Leonardo
8 Brad
If i have id's in order of (7,5,1,3) and i want to select limit 6 then the expected output should be as following :-
id Name
7 | Leonardo
5 | Messi
1 | John
3 | Daniel
2 | Dwayne
4 | Ronaldo
What i did previously was this but did not get the expected output . Help!!
"select * from users ORDER BY FIELD(user_id,7,5,1,3) LIMIT 6"
Your query is close. It just doesn't take into account all the other user ids. Try this:
ORDER BY FIELD(user_id,7,5,1,3) > 0 desc,
FIELD(user_id,7,5,1,3)
LIMIT 6;
The problem with your version is that field() returns 0 when there is no match. And the 0s will be ordered first.

mysql: ORDER BY number of occurrences of an element in a column of mysql table

Can someone please help me to 'ORDER' a mysql table according to the no. of occurrences of a string in one of the columns of same table.
I want to rearrange the table according to the number of times a location has occurred.
Please see the example below:
id name location
-- ----- --------
1 Mark US
2 Mike US
3 Paul Australia
4 Pranshu India
5 Pranav India
6 John Canada
7 Rishab India
Expected result:
id name location
-- ----- --------
4 Pranshu India
5 Pranav India
7 Rishab India
1 Mark US
2 Mike US
3 Paul Australia
6 John Canada
I tried this query:
SELECT *, COUNT(location) AS count FROM `tablename` GROUP BY `location` ORDER BY count DESC;
But it showed me the following result omitting the repeating occurrence of location:
id name location
-- ----- --------
4 Pranshu India
1 Mark US
3 Paul Australia
6 John Canada
SELECT x.*
FROM my_table x
JOIN (SELECT location, COUNT(*) total FROM my_table GROUP BY location) y
ON y.location = x.location
ORDER
BY total DESC
, id;

Sorting mysql by number of occurrences

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.