开发者

Retrieve all tags for all results in MYSQL Query

开发者 https://www.devze.com 2023-03-17 00:03 出处:网络
I am working on a video tagging system. I know the unique ID for each video I want in the search result thanks to a previous query I have to run, but what I want to do is show all of the tags for each

I am working on a video tagging system. I know the unique ID for each video I want in the search result thanks to a previous query I have to run, but what I want to do is show all of the tags for each video in the search result in the listing so it's multiple categories and contributors are displayed without having to click through into the main page.

The code below returns 5 rows for the 2 videos in the search results. I can always loop around that result, but then that means that I can't use the limit functions and I will have to build some php to do that for me.

SELECT * FROM tags
JOIN siteupdates ON tags.item_id = siteupdates.clip_id
JOIN updatetyp开发者_StackOverflowe ON siteupdates.updatetype = updatetype.id_type
JOIN tagnames ON tags.tag = tagnames.tagid
WHERE tags.item_id IN (248,257)
ORDER BY siteupdates.clip_date DESC

Result (this is an example of the first 5 results, and I have cut out most of the columns that aren't relevant)

uid / item_id / tag / updatetype / tagname / tagtype
1560 / 248 / 14 / 1 / tag 14 / Category
1561 / 248 / 3 / 1 / tag 3 / Contributor
1562 / 248 / 7 / 1 / tag 7 / Category
543 / 257 / 43 / 1 / tag 43 / Category
544 / 257 / 3 / 1 / tag 3 / Contributor

Ideally what I need is a 2 row result, but containing all of the tag data.

alternatively, am I just asking for something that isn't possible just with just MYSQL and my original plan to loop around in PHP is the only option? A new query to get the tag info for each result does not sound like a good idea to me as it will be possible to have up to 50 results per page, so 52 queries in total (including one earlier in the script)!

Any help is greatly appreciated.


So if I get you right you want all of the tags, but not one tag per result record but all in one? In this case GROUP_CONCAT() might help.

0

精彩评论

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