0

这是我的数据库架构:

Post:
id
title
body
date

Tag:
id
title

Post_Tag:
id
id_post
id_tag

Comment:
id
id_post
body
date

这是我的查询:

SELECT
    Post.id AS post_id,
    Post.title AS post_title,
    Post.body AS post_body,
    GROUP_CONCAT(CONCAT(Tag.id, "|", Tag.title) SEPARATOR '#') AS tags,
    COUNT(Comment.id) AS comment_count
FROM Post
LEFT JOIN Comment ON Post.id = Comment.id_post
LEFT JOIN Post_Tag ON Post.id = Post_Tag.id_post
LEFT JOIN Tag ON Tag.id = Post_Tag.id_tag
GROUP BY Post.id
ORDER BY Post.date ASC

有人可以告诉我为什么我在标签列下面得到这些奇怪的结果([BLOB-...])?

id  title           body            tags            comment_count
1   hello guys  blablabla...    [BLOB - 8B]         8
2   hello all   blablabla...    [BLOB - 14B]        3
3   how to tell blablabla...    [BLOB - 8B]         5
4   hello world blablabla...    [BLOB - 5B]         7
4

3 回答 3

1

这是一个比较知名的配置问题:yourgroup_concat_max_len设置为一个很大的值,迫使 MySql 使用 BLOBs 而不是varchars 的结果group_concat

要修复,设置group_concat_max_len为,比如说,512在您的my.iniormy.cnf文件中,然后重新启动 MySql。

这是包含更多信息的帖子的链接

于 2013-05-17T09:16:54.803 回答
0

您还可以使用CONVERT()BLOB 数据并将其转换为utf8

CONVERT(GROUP_CONCAT(CONCAT(Tag.id, "|", Tag.title) SEPARATOR '#') AS tags USING utf8),
于 2013-05-17T10:31:37.927 回答
0

代替:

GROUP_CONCAT(CONCAT(Tag.id, "|", Tag.title) SEPARATOR '#') AS tags,

和:

CAST(GROUP_CONCAT(CONCAT(Tag.id, "|", Tag.title) SEPARATOR '#') AS CHAR) AS tags,

文档:CAST 函数

于 2013-05-17T09:16:30.033 回答