I have a MySQL table StudentName(id,name) that looks like this:
id | name
++++++++
1 | alex
1 | adam
1 | adnan
2 | ben
2 | bush
3 | cris
4 | daisi
4 | diana
And I'd like to make a new table like this:
id | name
+++++++++++
1 | alex, adam, adnan
2 | ben, bush
3 | cris
4 | daisi, diana
Is there a way to accomplish this?
The group_concat function is what you're looking for:
SELECT id, GROUP_CONCAT(name ORDER BY name ASC SEPARATOR ', ')
FROM my_table
GROUP BY id
Related
MYSQL - How to count the first name of each line? I have a customer table that has a column with the full name of the customer. I need to know the number of characters in each customer's first name
------------------------
| table client |
------------------------
| id | name |
------------------------
| 1 | Dylan Smith |
| 2 | Bruce Johnson |
| 3 | James Williams |
| 4 | Thomas Johnson |
| 5 | Jimmy Jones |
| 6 | Matthew Miller |
------------------------
I need the query to return the following:
id | qtd_caracte
1 | 5
2 | 5
3 | 5
4 | 6
5 | 5
6 | 7
Does anyone know how this is possible? already searched on google and did not find.
One method uses substring_index():
select t.*, char_length(substring_index(name, ' ', 1))
from t;
Note this conveniently works even if the name does not have a space, which is why I suggest it over position()/instr()/locate().
Use string functions:
select id, char_length(substring_index(name, ' ', 1)) nb_car
from mytable
All you need is the function LOCATE() to get the position of the first space:
select id,
locate(' ', name) - 1 qtd_caracte
from client
If there is a case that the name does not contain a space:
select id,
locate(' ', concat(name, ' ')) - 1 qtd_caracte
from client
See the demo.
Results:
> id | qtd_caracte
> -: | ----------:
> 1 | 5
> 2 | 5
> 3 | 5
> 4 | 6
> 5 | 5
> 6 | 7
Try this:
SELECT id, char_length(SUBSTRING_INDEX(`name`, ' ', 1)) AS `qtd_caracte` from client ;
Enjoy...
I have two independent tables: 'Clients' and 'Country'.
Country Table:
IdCountry Country
1 SPAIN
2 PORTUGAL
Clients Table
IdClient Entity IdCountry
1 Adam Alves 2
2 Peter Smith 2
3 David Ramos 1
4 Rafael Castro 1
I would like to add a new client into 'Clients' table but using the information from 'Country' table like this:
INSERT INTO Clients(IdClient, Entity, Country)
SELECT max(IdClient) + 1, '--New--' FROM Clients,
SELECT IdCountry FROM Country WHERE Country = 'SPAIN'
I would like to have this INPUT:
IdClient Entity IdCountry
5 --New-- 1
But if I run this query, it doesn't work. Could anybody help me, please?
COMMENTS: I prefer don't use autoincrement option.
Thank you very much.
Wardiam
You can do it like this:
INSERT INTO Clients(IdClient, Entity, Country)
SELECT
(SELECT MAX(IdClient) + 1 FROM Clients),
'--New--',
(SELECT IdCountry FROM Country WHERE Country = 'SPAIN')
See the demo.
Results:
| IdClient | Entity | Country |
| -------- | ------------- | ------- |
| 1 | Adam Alves | 2 |
| 2 | Peter Smith | 2 |
| 3 | David Ramos | 1 |
| 4 | Rafael Castro | 1 |
| 5 | --New-- | 1 |
I need help. I can't seem to find the logic behind this code.
I am working on a voting system, and I need to output the results of the votes.
I want to count all of the rows that has a unique name in it and output how many.
My table goes like this.
voterid | pres | vpres | sec | trea | PIO
---------------------------------------------
1 | John | Mitch | James | Jack | Eman
2 | John | Pao | Bryan | Jack | Faye
3 | Kelvin | Pao | James | Jeck | Faye
Output should be
Pres | Votes
--------------
John | 2
Kelvin | 1
Here's my code.
SELECT DISTINCT
pres,
(SELECT COUNT(pres) FROM (SELECT DISTINCT pres FROM tblVote AS Votes)) AS Votes
FROM tblVote
Thanks in advance!
I think you are just looking for a simple GROUP BY query:
SELECT pres, COUNT(*) AS Votes
FROM tblVote
GROUP BY pres
I have a table like this:
name | day | score
------------------
John | 1 | 4
John | 2 | 5
John | 3 | 6
Marc | 1 | 7
Marc | 2 | 4
Marc | 3 | 5
Paul | 1 | 8
Paul | 2 | 2
Paul | 3 | 3
I want to get the sum of the score for each person, but only for certain days, sorted by this sum. let's say I want to get the score-sum of the 1. and 2. day, this is what I expect:
name | sum(score)
-----------------
Marc | 11
Paul | 10
John | 9
this is what failed:
SELECT name, sum(score) FROM mytable WHERE day<=2
I think I have to surround the sum(score)-part with some IF-statement, but I have no idea how.
Just add group by
SELECT name, sum(score) FROM mytable WHERE day<=2 group by name
Use sum function and group by clause for grouping the result.
query
select name,sum(score) as score
from myTable
where day in (1,2)
group by name
order by sum(score) desc;
fiddle demo
What I'm hoping to do is create a string out of a table WITHIN a query so that I may be able to place that string in another query I'm creating. Say, I have this for a table:
index | position | name
----------------------------------------
1 | member | John Smith
2 | chair | Mary Jones
3 | member | Mary Jones
4 | contact | Grace Adams
5 | director | Grace Adams
6 | member | Grace Adams
7 | treasurer | Bill McDonnell
8 | vice chair | Bill McDonnell
9 | member | Ishmael Rodriguez
I'm looking for the result as follows:
name | positions
----------------------------------------
John Smith | member
Mary Jones | chair,member
Grace Adams | contact,director,member
Bill McDonnell | treasurer,vice chair
Ishmael Rodriguez | member
I was hoping I could use some variant of CONCAT_WS() to get my result, like this...
SELECT
a.NAME,
CONCAT_WS(
',',
(
SELECT
position
FROM
TABLE
WHERE
NAME = a.NAME
)
)AS positions FROM ---------------
Obviously, this isn't working out for me. Any ideas?
Use GROUP_CONCAT[docs]
SELECT name, GROUP_CONCAT(position) result
FROM tableName
GROUP BY name
ORDER BY `index`
SQLFiddle Demo
Use GROUP_CONCAT like so:
SELECT name, GROUP_CONCAT(position SEPARATOR ',')
FROM Table
GROUP BY name