这是我的数据库架构:
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
答案 0 :(得分:1)
这是一个相对众所周知的配置问题:您的group_concat_max_len
设置为较大的值,迫使MySql使用BLOB而不是varchar
来获取group_concat
的结果。< / p>
要解决此问题,请在group_concat_max_len
或512
文件中将my.ini
设置为my.cnf
,然后重新启动MySql。
答案 1 :(得分:0)
替换:
GROUP_CONCAT(CONCAT(Tag.id, "|", Tag.title) SEPARATOR '#') AS tags,
使用:
CAST(GROUP_CONCAT(CONCAT(Tag.id, "|", Tag.title) SEPARATOR '#') AS CHAR) AS tags,
答案 2 :(得分:0)
您也可以使用CONVERT()
并将BLOB数据转换为utf8
CONVERT(GROUP_CONCAT(CONCAT(Tag.id, "|", Tag.title) SEPARATOR '#') AS tags USING utf8),