开发者

Getting GROUP_CONCAT values as individual values

I'm trying to implement a simple tagging system (messing around in php)...

I use the following sql command to get the required thread, its author and the tags associated with it:

$thread = select_Query("SELECT thread.title, thread.id as t_id,
                         thread.content, author.username, author.id as a_id,
                         GROUP_CONCAT(DISTINCT tags.name ORDER BY tags.name DESC SEPARATOR ',') AS tags
                         FROM thread JOIN thread_tags ON thread.id = thread_tags.thread_id
                         JOIN tags ON thread_tags.tag_id = tags.id
                         INNER JOIN author on author.id = thread.author_id
                         WHERE thread.id = $id", $link);

As you can see, I am using GROUP_CONCAT. This works fine, however when I do this, the tags all appear in one variable and I know I can use $pieces = explode(",", $thread['tags]); However is there another way of doing this? I am asking this because tags are easy to sepa开发者_如何转开发rate however if it was something more complex (e.g. something that contain the delimiter ,).

My database schema is as follows:

thread: id, content, title, author_id

thread_tags: id, tag_id, thread_id

tags: id, name


You can change the delimiter that GROUP_CONCAT uses to something that won't appear in the tag, maybe the '|' character.

GROUP_CONCAT(DISTINCT tags.name ORDER BY tags.name DESC SEPARATOR '|')

You can also validate the tag field and make sure that it does not contain the delimiter.


Don't try to squish multiple values into a single cell. This isn't how SQL was designed to be used and as you correctly point out it can cause problems if your separator appears as one of the values.

Luckily there is a way to solve this by rewriting your query. The solution is to return multiple rows instead of multiple values in a cell. Effectively you are normalizing your result set.

In other words, instead of this:

1 tag_1,tag_2,tag_3
2 tag_2,tag_4,tag_5

Do this:

1 tag_1
1 tag_2
1 tag_3
2 tag_2
2 tag_4
2 tag_5

Doing this naïvely can result in duplicating the same values for the other columns, which can be wasting bandwidth unnecessarily if those columns could potentially contain a lot of data. The solution to this is to use two queries.

In other words instead of this:

1 foo bar tag_1
1 foo bar tag_2
1 foo bar tag_3
2 baz qux tag_2
2 baz qux tag_4
2 baz qux tag_5

Do this:

Result 1:
1 foo bar
2 baz qux

Result 2:
1 tag_1
1 tag_2
1 tag_3
2 tag_2
2 tag_4
2 tag_5

Now your result set is in normal form. Also note that you no longer need to run a DISTINCT operation as your results already are distinct. In fact these two queries much more closely match your original database tables so the queries can be much simpler and it will run faster.


Most tagging systems have a fixed set of requirements for what characters can or cannot be in tags. You could just disallow , characters in your tags, and it'd work fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜