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.










share|improve this question






















  • 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















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.










share|improve this question






















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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






share|improve this answer





















  • 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















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






share|improve this answer





















  • 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













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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'