0

I use the MySQL GROUP_CONCAT function is an aggregate function that concatenates strings from a group into a single string with various options for this return

'31A', '31C', '32B', '32D', '52G'

This is the query:

SELECT 
  GROUP_CONCAT( DISTINCT CONCAT( '''', xCOD ) ORDER BY xCOD ASC SEPARATOR ''' , ' ) AS x_group 
FROM `xtbl`
ORDER BY xCod ASC;

This returns:

'31A', '31C', '32B', '32D', '52G

But on the return the last element of MySQL GROUP_CONCAT function don't have the single quote

How to do resolve this?

6
  • 2
    What is your question? There is none here, which makes your post really unclear. Commented Dec 7, 2020 at 16:49
  • @GMB thanks can't add on this question the code of my query. Error submitted question. I have added query code image... An error occurred submitting the edit. Commented Dec 7, 2020 at 16:52
  • You can always add a quote if you need it at the end, using CONCAT(). Commented Dec 7, 2020 at 16:52
  • @IterLsicIealf Don't post an image. Post your query (and results) as text. Commented Dec 7, 2020 at 16:52
  • @RocketHazmat thanksI can't add on this question the code of my query. Error submitted question. I have added query code image.. An error occurred submitting the edit. Commented Dec 7, 2020 at 16:53

1 Answer 1

2

If you want to wrap each value in quotes, then you can just use CONCAT for this.

SELECT 
  GROUP_CONCAT(DISTINCT CONCAT("'", xCOD, "'") ORDER BY xCOD ASC SEPARATOR ", ") AS x_group 
FROM `xtbl`
ORDER BY xCod ASC;

In fact, you don't even need CONCAT here at all. You can just do:

SELECT 
  GROUP_CONCAT(DISTINCT "'", xCOD, "'" ORDER BY xCOD ASC SEPARATOR ", ") AS x_group 
FROM `xtbl`
ORDER BY xCod ASC;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.