开发者

Merging MySQL row entries into a single row

开发者 https://www.devze.com 2022-12-29 15:06 出处:网络
I\'ve got two tables, one for listings and another representing a list of tags for the listings table.

I've got two tables, one for listings and another representing a list of tags for the listings table.

In the listings table the tag ids are stored in a field called tags as 1-2-3-. This has worked out very well for me (regular expressions and joins to separate and display the data), but I now need to pull the titles of those tags into a single row. See below.

listings table
id    tags
1     1-2-3-
2     4-5-6-


tags table
id    title
1     pig
2     dog
3     cat
4     mouse
5     elephant
6     duck

And what I need to produce out of the listings table is:

id     tags
2      mouse, elepha开发者_JS百科nt, duck


Here is a query that could help. But since it is doing some string operations, it may not be as good as a regular join:

select l.id, group_concat( t.title ) 
   from listings l, tags t 
   where concat( '-', l.tags )  like concat( '%-', t.id, '-%' ) group by l.id ;


Unfortunately, with your tags stored in this denormalized format, there's no easy way to go from 1-2-3 to the corresponding tags. In other words, there's no simple way to split out the ids, join to another table and then recombine. Your best option would be to create a listing_tag table with two columns

listing_id    tag_id
1             1
1             2
1             3
2             4
2             5
2             6

and then it's just a simple join:

SELECT listing_id, GROUP_CONCAT(title SEPARATOR ', ') FROM listing_tag JOIN tags ON tags.id = tag_id


GROUP_CONCAT() + INNER JOIN + GROUP BY

0

精彩评论

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