Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 120729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:51:27+00:00 2026-05-11T03:51:27+00:00

(NOTE: This question is not about escaping queries, it’s about escaping results) I’m using

  • 0

(NOTE: This question is not about escaping queries, it’s about escaping results)

I’m using GROUP_CONCAT to combine multiple rows into a comma delimited list. For example, assume I have the two (example) tables:

CREATE TABLE IF NOT EXISTS `Comment` ( `id` int(11) unsigned NOT NULL auto_increment, `post_id` int(11) unsigned NOT NULL, `name` varchar(255) collate utf8_unicode_ci NOT NULL, `comment` varchar(255) collate utf8_unicode_ci NOT NULL, PRIMARY KEY  (`id`), KEY `post_id` (`post_id`) ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;  INSERT INTO `Comment` (`id`, `post_id`, `name`, `comment`) VALUES (1, 1, 'bill', 'some comment'), (2, 1, 'john', 'another comment'), (3, 2, 'bill', 'blah'), (4, 3, 'john', 'asdf'), (5, 4, 'x', 'asdf');   CREATE TABLE IF NOT EXISTS `Post` ( `id` int(11) NOT NULL auto_increment, `title` varchar(255) collate utf8_unicode_ci NOT NULL, PRIMARY KEY  (`id`) ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;  INSERT INTO `Post` (`id`, `title`) VALUES (1, 'first post'), (2, 'second post'), (3, 'third post'), (4, 'fourth post'), (5, 'fifth post'), (6, 'sixth post'); 

And I want to list all posts along with a list of each username who commented on the post:

SELECT Post.id as post_id, Post.title as title, GROUP_CONCAT(name)  FROM Post  LEFT JOIN Comment on Comment.post_id = Post.id GROUP BY Post.id 

gives me:

id  title   GROUP_CONCAT( name ) 1   first post  bill,john 2   second post     bill 3   third post  john 4   fourth post     x 5   fifth post  NULL 6   sixth post  NULL 

This works great, except that if a username contains a comma it will ruin the list of users. Does MySQL have a function that will let me escape these characters? (Please assume usernames can contain any characters, since this is only an example schema)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:51:27+00:00Added an answer on May 11, 2026 at 3:51 am

    If there’s some other character that’s illegal in usernames, you can specify a different separator character using a little-known syntax:

    ...GROUP_CONCAT(name SEPARATOR '|')... 

    … You want to allow pipes? or any character?

    Escape the separator character, perhaps with backslash, but before doing that escape backslashes themselves:

    group_concat(replace(replace(name, '\\', '\\\\'), '|', '\\|') SEPARATOR '|') 

    This will:

    1. escape any backslashes with another backslash
    2. escape the separator character with a backslash
    3. concatenate the results with the separator character

    To get the unescaped results, do the same thing in the reverse order:

    1. split the results by the separator character where not preceded by a backslash. Actually, it’s a little tricky, you want to split it where it isn’t preceded by an odd number of blackslashes. This regex will match that:
      (?<!\\)(?:\\\\)*\|
    2. replace all escaped separator chars with literals, i.e. replace \| with |
    3. replace all double backslashes with singe backslashes, e.g. replace \\ with \
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Your question is about inheritance and interfaces. The following is… May 11, 2026 at 3:50 pm
  • added an answer Try these rules: RewriteCond %{HTTP_HOST} ^dev\. RewriteCond %{HTTP_HOST} !^dev\.askia\.com$ RewriteRule… May 11, 2026 at 3:50 pm
  • added an answer The reason there are gaps is due to rounding errors.… May 11, 2026 at 3:50 pm

Related Questions

(NOTE: This question is not about escaping queries, it's about escaping results) I'm using
I used to use the standard mysql_connect(), mysql_query(), etc statements for doing MySQL stuff
We are localising the user-interface text for a web application that runs on Java
I recently asked about new languages and language features of the last 10 years
If I have an existing database, I want to be able to automatically code

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.