mySQL query returns 3 columns, I want to get values from one column concatenated into comma separated string
up vote
0
down vote
favorite
I need to get the values from the meta_value column that have "shipping" in the corresponding meta_key column and concatenate them into a comma separated row.
I'm a bit of a noob to SQL so please be gentle.

My current query is this:
SELECT wp_postmeta.post_ID, wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.post_status
FROM wp_postmeta
CROSS JOIN wp_posts
WHERE wp_postmeta.post_ID= wp_posts.ID
AND wp_posts.post_status LIKE 'wc-processing'
AND (wp_postmeta.meta_key LIKE '%shipping%' OR wp_postmeta.meta_key LIKE '_order_total')
mysql string concatenation
add a comment |
up vote
0
down vote
favorite
I need to get the values from the meta_value column that have "shipping" in the corresponding meta_key column and concatenate them into a comma separated row.
I'm a bit of a noob to SQL so please be gentle.

My current query is this:
SELECT wp_postmeta.post_ID, wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.post_status
FROM wp_postmeta
CROSS JOIN wp_posts
WHERE wp_postmeta.post_ID= wp_posts.ID
AND wp_posts.post_status LIKE 'wc-processing'
AND (wp_postmeta.meta_key LIKE '%shipping%' OR wp_postmeta.meta_key LIKE '_order_total')
mysql string concatenation
can you please provide some snippets of your code?
– nyx97
Nov 12 at 1:30
You don't want a cross join (which is also called a cartesian product). That gives you the number of records in the first table X the number of records in the 2nd table. You probably want an inner join, which gives you matching records. FYI, the remaining joins are left, right and full. There's a nice Venn diagram out there: blog.codinghorror.com/a-visual-explanation-of-sql-joins
– kermit
Nov 12 at 1:36
What will be expected output, as per your given sample data ?
– Madhur Bhaiya
Nov 12 at 7:41
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 17 at 2:18
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to get the values from the meta_value column that have "shipping" in the corresponding meta_key column and concatenate them into a comma separated row.
I'm a bit of a noob to SQL so please be gentle.

My current query is this:
SELECT wp_postmeta.post_ID, wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.post_status
FROM wp_postmeta
CROSS JOIN wp_posts
WHERE wp_postmeta.post_ID= wp_posts.ID
AND wp_posts.post_status LIKE 'wc-processing'
AND (wp_postmeta.meta_key LIKE '%shipping%' OR wp_postmeta.meta_key LIKE '_order_total')
mysql string concatenation
I need to get the values from the meta_value column that have "shipping" in the corresponding meta_key column and concatenate them into a comma separated row.
I'm a bit of a noob to SQL so please be gentle.

My current query is this:
SELECT wp_postmeta.post_ID, wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.post_status
FROM wp_postmeta
CROSS JOIN wp_posts
WHERE wp_postmeta.post_ID= wp_posts.ID
AND wp_posts.post_status LIKE 'wc-processing'
AND (wp_postmeta.meta_key LIKE '%shipping%' OR wp_postmeta.meta_key LIKE '_order_total')
mysql string concatenation
mysql string concatenation
edited Nov 12 at 1:50
Nick
22.3k81535
22.3k81535
asked Nov 12 at 1:22
user10638179
12
12
can you please provide some snippets of your code?
– nyx97
Nov 12 at 1:30
You don't want a cross join (which is also called a cartesian product). That gives you the number of records in the first table X the number of records in the 2nd table. You probably want an inner join, which gives you matching records. FYI, the remaining joins are left, right and full. There's a nice Venn diagram out there: blog.codinghorror.com/a-visual-explanation-of-sql-joins
– kermit
Nov 12 at 1:36
What will be expected output, as per your given sample data ?
– Madhur Bhaiya
Nov 12 at 7:41
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 17 at 2:18
add a comment |
can you please provide some snippets of your code?
– nyx97
Nov 12 at 1:30
You don't want a cross join (which is also called a cartesian product). That gives you the number of records in the first table X the number of records in the 2nd table. You probably want an inner join, which gives you matching records. FYI, the remaining joins are left, right and full. There's a nice Venn diagram out there: blog.codinghorror.com/a-visual-explanation-of-sql-joins
– kermit
Nov 12 at 1:36
What will be expected output, as per your given sample data ?
– Madhur Bhaiya
Nov 12 at 7:41
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 17 at 2:18
can you please provide some snippets of your code?
– nyx97
Nov 12 at 1:30
can you please provide some snippets of your code?
– nyx97
Nov 12 at 1:30
You don't want a cross join (which is also called a cartesian product). That gives you the number of records in the first table X the number of records in the 2nd table. You probably want an inner join, which gives you matching records. FYI, the remaining joins are left, right and full. There's a nice Venn diagram out there: blog.codinghorror.com/a-visual-explanation-of-sql-joins
– kermit
Nov 12 at 1:36
You don't want a cross join (which is also called a cartesian product). That gives you the number of records in the first table X the number of records in the 2nd table. You probably want an inner join, which gives you matching records. FYI, the remaining joins are left, right and full. There's a nice Venn diagram out there: blog.codinghorror.com/a-visual-explanation-of-sql-joins
– kermit
Nov 12 at 1:36
What will be expected output, as per your given sample data ?
– Madhur Bhaiya
Nov 12 at 7:41
What will be expected output, as per your given sample data ?
– Madhur Bhaiya
Nov 12 at 7:41
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 17 at 2:18
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 17 at 2:18
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
So, here's your query (note how I write it - I find that good formatting of SQL helps to read it):
SELECT
wp_postmeta.post_ID,
wp_postmeta.meta_key,
wp_postmeta.meta_value,
wp_posts.ID,
wp_posts.post_status
FROM
wp_postmeta
CROSS JOIN
wp_posts
WHERE
wp_postmeta.post_ID = wp_posts.ID
AND wp_posts.post_status LIKE 'wc-processing'
AND (wp_postmeta.meta_key LIKE '%shipping%' OR wp_postmeta.meta_key LIKE '_order_total')
Here's what I think you want (you use the MySQL concat function):
Select
concat(
wp_postmeta.meta_key, ',',
wp_postmeta.meta_value
) as formatted_row
FROM
wp_postmeta
INNER JOIN
wp_posts
ON
wp_postmeta.post_ID = wp_posts.ID
WHERE
wp_posts.post_status LIKE 'wc-processing'
AND (
wp_postmeta.meta_key LIKE '%shipping%'
OR wp_postmeta.meta_key LIKE '_order_total'
)
Also note: Single quotes and double quotes mean different things in different databases.
add a comment |
up vote
0
down vote
You can use GROUP_CONCAT to join the strings together. As has been pointed out by @kermit, you should use an INNER JOIN not a CROSS JOIN. I am presuming you want to keep the order total separate from the shipping details, so you will need to use conditional aggregation as well. This query should do what you want:
SELECT m.post_ID
, GROUP_CONCAT(CASE WHEN m.meta_key LIKE '%shipping%' THEN m.meta_value END) AS shipping_info
, MAX(CASE WHEN m.meta_key = '_order_total' THEN m.meta_value ELSE 0 END) AS order_total
FROM wp_postmeta m
INNER JOIN wp_posts p ON m.post_ID = p.ID
WHERE p.post_status LIKE 'wc-processing'
AND (m.meta_key LIKE '%shipping%' OR m.meta_key LIKE '_order_total')
add a comment |
up vote
0
down vote
this should do the trick its a bit more involved than on sql server but easy enough
create table if not exists test(col1 varchar(10),col2 varchar(10), col3 varchar(10));
insert into test values("this","is","test");
select concat(col1,",",col2,",",col3) as csvCol from test
delivers
csvCol
this,is,test
Apologies all for my lack of response. I got snowed under at work and haven't had time to test these out. I will endeavour to do so in the next couple of days and will respond then. Thanks for all your help.
– user10638179
Nov 25 at 1:16
add a comment |
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
);
);
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%2f53254902%2fmysql-query-returns-3-columns-i-want-to-get-values-from-one-column-concatenated%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
So, here's your query (note how I write it - I find that good formatting of SQL helps to read it):
SELECT
wp_postmeta.post_ID,
wp_postmeta.meta_key,
wp_postmeta.meta_value,
wp_posts.ID,
wp_posts.post_status
FROM
wp_postmeta
CROSS JOIN
wp_posts
WHERE
wp_postmeta.post_ID = wp_posts.ID
AND wp_posts.post_status LIKE 'wc-processing'
AND (wp_postmeta.meta_key LIKE '%shipping%' OR wp_postmeta.meta_key LIKE '_order_total')
Here's what I think you want (you use the MySQL concat function):
Select
concat(
wp_postmeta.meta_key, ',',
wp_postmeta.meta_value
) as formatted_row
FROM
wp_postmeta
INNER JOIN
wp_posts
ON
wp_postmeta.post_ID = wp_posts.ID
WHERE
wp_posts.post_status LIKE 'wc-processing'
AND (
wp_postmeta.meta_key LIKE '%shipping%'
OR wp_postmeta.meta_key LIKE '_order_total'
)
Also note: Single quotes and double quotes mean different things in different databases.
add a comment |
up vote
0
down vote
So, here's your query (note how I write it - I find that good formatting of SQL helps to read it):
SELECT
wp_postmeta.post_ID,
wp_postmeta.meta_key,
wp_postmeta.meta_value,
wp_posts.ID,
wp_posts.post_status
FROM
wp_postmeta
CROSS JOIN
wp_posts
WHERE
wp_postmeta.post_ID = wp_posts.ID
AND wp_posts.post_status LIKE 'wc-processing'
AND (wp_postmeta.meta_key LIKE '%shipping%' OR wp_postmeta.meta_key LIKE '_order_total')
Here's what I think you want (you use the MySQL concat function):
Select
concat(
wp_postmeta.meta_key, ',',
wp_postmeta.meta_value
) as formatted_row
FROM
wp_postmeta
INNER JOIN
wp_posts
ON
wp_postmeta.post_ID = wp_posts.ID
WHERE
wp_posts.post_status LIKE 'wc-processing'
AND (
wp_postmeta.meta_key LIKE '%shipping%'
OR wp_postmeta.meta_key LIKE '_order_total'
)
Also note: Single quotes and double quotes mean different things in different databases.
add a comment |
up vote
0
down vote
up vote
0
down vote
So, here's your query (note how I write it - I find that good formatting of SQL helps to read it):
SELECT
wp_postmeta.post_ID,
wp_postmeta.meta_key,
wp_postmeta.meta_value,
wp_posts.ID,
wp_posts.post_status
FROM
wp_postmeta
CROSS JOIN
wp_posts
WHERE
wp_postmeta.post_ID = wp_posts.ID
AND wp_posts.post_status LIKE 'wc-processing'
AND (wp_postmeta.meta_key LIKE '%shipping%' OR wp_postmeta.meta_key LIKE '_order_total')
Here's what I think you want (you use the MySQL concat function):
Select
concat(
wp_postmeta.meta_key, ',',
wp_postmeta.meta_value
) as formatted_row
FROM
wp_postmeta
INNER JOIN
wp_posts
ON
wp_postmeta.post_ID = wp_posts.ID
WHERE
wp_posts.post_status LIKE 'wc-processing'
AND (
wp_postmeta.meta_key LIKE '%shipping%'
OR wp_postmeta.meta_key LIKE '_order_total'
)
Also note: Single quotes and double quotes mean different things in different databases.
So, here's your query (note how I write it - I find that good formatting of SQL helps to read it):
SELECT
wp_postmeta.post_ID,
wp_postmeta.meta_key,
wp_postmeta.meta_value,
wp_posts.ID,
wp_posts.post_status
FROM
wp_postmeta
CROSS JOIN
wp_posts
WHERE
wp_postmeta.post_ID = wp_posts.ID
AND wp_posts.post_status LIKE 'wc-processing'
AND (wp_postmeta.meta_key LIKE '%shipping%' OR wp_postmeta.meta_key LIKE '_order_total')
Here's what I think you want (you use the MySQL concat function):
Select
concat(
wp_postmeta.meta_key, ',',
wp_postmeta.meta_value
) as formatted_row
FROM
wp_postmeta
INNER JOIN
wp_posts
ON
wp_postmeta.post_ID = wp_posts.ID
WHERE
wp_posts.post_status LIKE 'wc-processing'
AND (
wp_postmeta.meta_key LIKE '%shipping%'
OR wp_postmeta.meta_key LIKE '_order_total'
)
Also note: Single quotes and double quotes mean different things in different databases.
answered Nov 12 at 1:53
kermit
9941812
9941812
add a comment |
add a comment |
up vote
0
down vote
You can use GROUP_CONCAT to join the strings together. As has been pointed out by @kermit, you should use an INNER JOIN not a CROSS JOIN. I am presuming you want to keep the order total separate from the shipping details, so you will need to use conditional aggregation as well. This query should do what you want:
SELECT m.post_ID
, GROUP_CONCAT(CASE WHEN m.meta_key LIKE '%shipping%' THEN m.meta_value END) AS shipping_info
, MAX(CASE WHEN m.meta_key = '_order_total' THEN m.meta_value ELSE 0 END) AS order_total
FROM wp_postmeta m
INNER JOIN wp_posts p ON m.post_ID = p.ID
WHERE p.post_status LIKE 'wc-processing'
AND (m.meta_key LIKE '%shipping%' OR m.meta_key LIKE '_order_total')
add a comment |
up vote
0
down vote
You can use GROUP_CONCAT to join the strings together. As has been pointed out by @kermit, you should use an INNER JOIN not a CROSS JOIN. I am presuming you want to keep the order total separate from the shipping details, so you will need to use conditional aggregation as well. This query should do what you want:
SELECT m.post_ID
, GROUP_CONCAT(CASE WHEN m.meta_key LIKE '%shipping%' THEN m.meta_value END) AS shipping_info
, MAX(CASE WHEN m.meta_key = '_order_total' THEN m.meta_value ELSE 0 END) AS order_total
FROM wp_postmeta m
INNER JOIN wp_posts p ON m.post_ID = p.ID
WHERE p.post_status LIKE 'wc-processing'
AND (m.meta_key LIKE '%shipping%' OR m.meta_key LIKE '_order_total')
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use GROUP_CONCAT to join the strings together. As has been pointed out by @kermit, you should use an INNER JOIN not a CROSS JOIN. I am presuming you want to keep the order total separate from the shipping details, so you will need to use conditional aggregation as well. This query should do what you want:
SELECT m.post_ID
, GROUP_CONCAT(CASE WHEN m.meta_key LIKE '%shipping%' THEN m.meta_value END) AS shipping_info
, MAX(CASE WHEN m.meta_key = '_order_total' THEN m.meta_value ELSE 0 END) AS order_total
FROM wp_postmeta m
INNER JOIN wp_posts p ON m.post_ID = p.ID
WHERE p.post_status LIKE 'wc-processing'
AND (m.meta_key LIKE '%shipping%' OR m.meta_key LIKE '_order_total')
You can use GROUP_CONCAT to join the strings together. As has been pointed out by @kermit, you should use an INNER JOIN not a CROSS JOIN. I am presuming you want to keep the order total separate from the shipping details, so you will need to use conditional aggregation as well. This query should do what you want:
SELECT m.post_ID
, GROUP_CONCAT(CASE WHEN m.meta_key LIKE '%shipping%' THEN m.meta_value END) AS shipping_info
, MAX(CASE WHEN m.meta_key = '_order_total' THEN m.meta_value ELSE 0 END) AS order_total
FROM wp_postmeta m
INNER JOIN wp_posts p ON m.post_ID = p.ID
WHERE p.post_status LIKE 'wc-processing'
AND (m.meta_key LIKE '%shipping%' OR m.meta_key LIKE '_order_total')
answered Nov 12 at 2:01
Nick
22.3k81535
22.3k81535
add a comment |
add a comment |
up vote
0
down vote
this should do the trick its a bit more involved than on sql server but easy enough
create table if not exists test(col1 varchar(10),col2 varchar(10), col3 varchar(10));
insert into test values("this","is","test");
select concat(col1,",",col2,",",col3) as csvCol from test
delivers
csvCol
this,is,test
Apologies all for my lack of response. I got snowed under at work and haven't had time to test these out. I will endeavour to do so in the next couple of days and will respond then. Thanks for all your help.
– user10638179
Nov 25 at 1:16
add a comment |
up vote
0
down vote
this should do the trick its a bit more involved than on sql server but easy enough
create table if not exists test(col1 varchar(10),col2 varchar(10), col3 varchar(10));
insert into test values("this","is","test");
select concat(col1,",",col2,",",col3) as csvCol from test
delivers
csvCol
this,is,test
Apologies all for my lack of response. I got snowed under at work and haven't had time to test these out. I will endeavour to do so in the next couple of days and will respond then. Thanks for all your help.
– user10638179
Nov 25 at 1:16
add a comment |
up vote
0
down vote
up vote
0
down vote
this should do the trick its a bit more involved than on sql server but easy enough
create table if not exists test(col1 varchar(10),col2 varchar(10), col3 varchar(10));
insert into test values("this","is","test");
select concat(col1,",",col2,",",col3) as csvCol from test
delivers
csvCol
this,is,test
this should do the trick its a bit more involved than on sql server but easy enough
create table if not exists test(col1 varchar(10),col2 varchar(10), col3 varchar(10));
insert into test values("this","is","test");
select concat(col1,",",col2,",",col3) as csvCol from test
delivers
csvCol
this,is,test
answered Nov 12 at 2:03
quealegriamasalegre
12
12
Apologies all for my lack of response. I got snowed under at work and haven't had time to test these out. I will endeavour to do so in the next couple of days and will respond then. Thanks for all your help.
– user10638179
Nov 25 at 1:16
add a comment |
Apologies all for my lack of response. I got snowed under at work and haven't had time to test these out. I will endeavour to do so in the next couple of days and will respond then. Thanks for all your help.
– user10638179
Nov 25 at 1:16
Apologies all for my lack of response. I got snowed under at work and haven't had time to test these out. I will endeavour to do so in the next couple of days and will respond then. Thanks for all your help.
– user10638179
Nov 25 at 1:16
Apologies all for my lack of response. I got snowed under at work and haven't had time to test these out. I will endeavour to do so in the next couple of days and will respond then. Thanks for all your help.
– user10638179
Nov 25 at 1:16
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%2f53254902%2fmysql-query-returns-3-columns-i-want-to-get-values-from-one-column-concatenated%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
can you please provide some snippets of your code?
– nyx97
Nov 12 at 1:30
You don't want a cross join (which is also called a cartesian product). That gives you the number of records in the first table X the number of records in the 2nd table. You probably want an inner join, which gives you matching records. FYI, the remaining joins are left, right and full. There's a nice Venn diagram out there: blog.codinghorror.com/a-visual-explanation-of-sql-joins
– kermit
Nov 12 at 1:36
What will be expected output, as per your given sample data ?
– Madhur Bhaiya
Nov 12 at 7:41
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 17 at 2:18