mysql concat column from two rows based on a column
up vote
0
down vote
favorite
Have the following table which i am trying to concat two rows from the 'value' column into one
mysql> select * from table;
+-----+---------+----------+------------------------+
| id | rsvp_id | field_id | value |
+-----+---------+----------+------------------------+
| 181 | 37 | 1 | First |
| 184 | 37 | 4 | Last |
| 187 | 37 | 10 | |
| 190 | 37 | 13 | spicegirls |
| 193 | 37 | 7 | mark@test2.com |
| 196 | 40 | 1 | Brian |
| 199 | 40 | 1 | Smith |
| 202 | 40 | 7 | Brian@test .com |
| 205 | 40 | 10 | BBQ |
+-----+---------+----------+------------------------+
9 rows in set (0.00 sec)
Ideally i'd like to get the following result
rsvp_id | value
======== ========
37 First Last
40 Brian Smith
The query only grabs the rows with field_id=1 then concats the value column and creates a new row with rsvp_id and the concat value.
Also, the field_id column right now and the 1 is an example, i'll have to figure out how to make it work so instead of 1
it takes the condition from a different table.
Basically the above are values for first name and last name. field_id is a foreign_key to a different table.
I've tried searching online and messing with it myself but i wasn't able to merge the two rows into one row.
Thank You.
mysql
add a comment |
up vote
0
down vote
favorite
Have the following table which i am trying to concat two rows from the 'value' column into one
mysql> select * from table;
+-----+---------+----------+------------------------+
| id | rsvp_id | field_id | value |
+-----+---------+----------+------------------------+
| 181 | 37 | 1 | First |
| 184 | 37 | 4 | Last |
| 187 | 37 | 10 | |
| 190 | 37 | 13 | spicegirls |
| 193 | 37 | 7 | mark@test2.com |
| 196 | 40 | 1 | Brian |
| 199 | 40 | 1 | Smith |
| 202 | 40 | 7 | Brian@test .com |
| 205 | 40 | 10 | BBQ |
+-----+---------+----------+------------------------+
9 rows in set (0.00 sec)
Ideally i'd like to get the following result
rsvp_id | value
======== ========
37 First Last
40 Brian Smith
The query only grabs the rows with field_id=1 then concats the value column and creates a new row with rsvp_id and the concat value.
Also, the field_id column right now and the 1 is an example, i'll have to figure out how to make it work so instead of 1
it takes the condition from a different table.
Basically the above are values for first name and last name. field_id is a foreign_key to a different table.
I've tried searching online and messing with it myself but i wasn't able to merge the two rows into one row.
Thank You.
mysql
What does your query look like?
– pmahomme
Nov 19 at 23:35
something like: group_concat(value, ' ') where field_id in (1,4) group by rsvp_id
– Used_By_Already
Nov 19 at 23:37
This is what i have so far: SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:16
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Have the following table which i am trying to concat two rows from the 'value' column into one
mysql> select * from table;
+-----+---------+----------+------------------------+
| id | rsvp_id | field_id | value |
+-----+---------+----------+------------------------+
| 181 | 37 | 1 | First |
| 184 | 37 | 4 | Last |
| 187 | 37 | 10 | |
| 190 | 37 | 13 | spicegirls |
| 193 | 37 | 7 | mark@test2.com |
| 196 | 40 | 1 | Brian |
| 199 | 40 | 1 | Smith |
| 202 | 40 | 7 | Brian@test .com |
| 205 | 40 | 10 | BBQ |
+-----+---------+----------+------------------------+
9 rows in set (0.00 sec)
Ideally i'd like to get the following result
rsvp_id | value
======== ========
37 First Last
40 Brian Smith
The query only grabs the rows with field_id=1 then concats the value column and creates a new row with rsvp_id and the concat value.
Also, the field_id column right now and the 1 is an example, i'll have to figure out how to make it work so instead of 1
it takes the condition from a different table.
Basically the above are values for first name and last name. field_id is a foreign_key to a different table.
I've tried searching online and messing with it myself but i wasn't able to merge the two rows into one row.
Thank You.
mysql
Have the following table which i am trying to concat two rows from the 'value' column into one
mysql> select * from table;
+-----+---------+----------+------------------------+
| id | rsvp_id | field_id | value |
+-----+---------+----------+------------------------+
| 181 | 37 | 1 | First |
| 184 | 37 | 4 | Last |
| 187 | 37 | 10 | |
| 190 | 37 | 13 | spicegirls |
| 193 | 37 | 7 | mark@test2.com |
| 196 | 40 | 1 | Brian |
| 199 | 40 | 1 | Smith |
| 202 | 40 | 7 | Brian@test .com |
| 205 | 40 | 10 | BBQ |
+-----+---------+----------+------------------------+
9 rows in set (0.00 sec)
Ideally i'd like to get the following result
rsvp_id | value
======== ========
37 First Last
40 Brian Smith
The query only grabs the rows with field_id=1 then concats the value column and creates a new row with rsvp_id and the concat value.
Also, the field_id column right now and the 1 is an example, i'll have to figure out how to make it work so instead of 1
it takes the condition from a different table.
Basically the above are values for first name and last name. field_id is a foreign_key to a different table.
I've tried searching online and messing with it myself but i wasn't able to merge the two rows into one row.
Thank You.
mysql
mysql
asked Nov 19 at 23:31
Vincent
32
32
What does your query look like?
– pmahomme
Nov 19 at 23:35
something like: group_concat(value, ' ') where field_id in (1,4) group by rsvp_id
– Used_By_Already
Nov 19 at 23:37
This is what i have so far: SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:16
add a comment |
What does your query look like?
– pmahomme
Nov 19 at 23:35
something like: group_concat(value, ' ') where field_id in (1,4) group by rsvp_id
– Used_By_Already
Nov 19 at 23:37
This is what i have so far: SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:16
What does your query look like?
– pmahomme
Nov 19 at 23:35
What does your query look like?
– pmahomme
Nov 19 at 23:35
something like: group_concat(value, ' ') where field_id in (1,4) group by rsvp_id
– Used_By_Already
Nov 19 at 23:37
something like: group_concat(value, ' ') where field_id in (1,4) group by rsvp_id
– Used_By_Already
Nov 19 at 23:37
This is what i have so far: SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:16
This is what i have so far: SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:16
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You have to use grouping and then use GROUP_CONCAT
.
Something like this (untested) might work:
SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ')
FROM t
WHERE field_id = 1
GROUP BY rsvp_id
See MySQL docs for details, also to learn about ordering of values etc:
https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
Hey, It seems this does exactly what i had before. the following query SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:15
output something like this rsvp_id | n 37 | Mark 38 | Dan 39 | John if i remove the GROUP BY rsvp_id i only get one column with the first name concated
– Vincent
Nov 21 at 0:15
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You have to use grouping and then use GROUP_CONCAT
.
Something like this (untested) might work:
SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ')
FROM t
WHERE field_id = 1
GROUP BY rsvp_id
See MySQL docs for details, also to learn about ordering of values etc:
https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
Hey, It seems this does exactly what i had before. the following query SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:15
output something like this rsvp_id | n 37 | Mark 38 | Dan 39 | John if i remove the GROUP BY rsvp_id i only get one column with the first name concated
– Vincent
Nov 21 at 0:15
add a comment |
up vote
1
down vote
You have to use grouping and then use GROUP_CONCAT
.
Something like this (untested) might work:
SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ')
FROM t
WHERE field_id = 1
GROUP BY rsvp_id
See MySQL docs for details, also to learn about ordering of values etc:
https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
Hey, It seems this does exactly what i had before. the following query SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:15
output something like this rsvp_id | n 37 | Mark 38 | Dan 39 | John if i remove the GROUP BY rsvp_id i only get one column with the first name concated
– Vincent
Nov 21 at 0:15
add a comment |
up vote
1
down vote
up vote
1
down vote
You have to use grouping and then use GROUP_CONCAT
.
Something like this (untested) might work:
SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ')
FROM t
WHERE field_id = 1
GROUP BY rsvp_id
See MySQL docs for details, also to learn about ordering of values etc:
https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
You have to use grouping and then use GROUP_CONCAT
.
Something like this (untested) might work:
SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ')
FROM t
WHERE field_id = 1
GROUP BY rsvp_id
See MySQL docs for details, also to learn about ordering of values etc:
https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
answered Nov 19 at 23:37
johannes
12.9k13354
12.9k13354
Hey, It seems this does exactly what i had before. the following query SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:15
output something like this rsvp_id | n 37 | Mark 38 | Dan 39 | John if i remove the GROUP BY rsvp_id i only get one column with the first name concated
– Vincent
Nov 21 at 0:15
add a comment |
Hey, It seems this does exactly what i had before. the following query SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:15
output something like this rsvp_id | n 37 | Mark 38 | Dan 39 | John if i remove the GROUP BY rsvp_id i only get one column with the first name concated
– Vincent
Nov 21 at 0:15
Hey, It seems this does exactly what i had before. the following query SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:15
Hey, It seems this does exactly what i had before. the following query SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:15
output something like this rsvp_id | n 37 | Mark 38 | Dan 39 | John if i remove the GROUP BY rsvp_id i only get one column with the first name concated
– Vincent
Nov 21 at 0:15
output something like this rsvp_id | n 37 | Mark 38 | Dan 39 | John if i remove the GROUP BY rsvp_id i only get one column with the first name concated
– Vincent
Nov 21 at 0:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384146%2fmysql-concat-column-from-two-rows-based-on-a-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What does your query look like?
– pmahomme
Nov 19 at 23:35
something like: group_concat(value, ' ') where field_id in (1,4) group by rsvp_id
– Used_By_Already
Nov 19 at 23:37
This is what i have so far: SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ') as n FROM registration_rsvp_field_value left join registration_rsvp_custom_field on (registration_rsvp_custom_field.id=registration_rsvp_field_value.field_id) left join registration_rsvp on (registration_rsvp.id=registration_rsvp_field_value.rsvp_id) WHERE field_id IN (1, 2) AND form_id=1 GROUP BY rsvp_id
– Vincent
Nov 21 at 0:16