mysql key 转义,MySQL GROUP_CONCAT转义

(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)

解决方案

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:

escape any backslashes with another backslash

escape the separator character with a backslash

concatenate the results with the separator character

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

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:

(?

replace all escaped separator chars with literals, i.e. replace \| with |

replace all double backslashes with singe backslashes, e.g. replace \\ with \

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值