how to decrease queries in php/mysql array selection loop
i need to show stories details and tags' names in my php/mysql project .
for every story row, there is a filed named : tags that save tags id as an array
Table name: stories
table filed : tags
example of tags filed :
1 5 6 space between them
and i have a tag table that looks like this
Table name : bt_tags
Table fileds : tid,tag
now problem :
when using while loop to fetch all fields in story table , the page uses 1 query to show every stories' detail but for showing tag's names , i should query another table to find names , we have ids stored in story table
now i used for loop between while开发者_如何学Python loop to show tag names but im sure there is a better way to decrease page queries
llowed to change anything in database table
how can i improve this script and show tag's names without using **for loop ?**
SELECT stories.storyid,
stories.storyname,
group_concat( tags.tagname )
FROM stories, tags
WHERE CONCAT( ' ', stories.tags, ' ' ) LIKE CONCAT( '%', tags.tagid, '%' )
GROUP BY stories.storyid, stories.storyname
Sounds like a join may help you
select s.*,
b.tag,
b.slug
from story_table s
left join bt_tags b on b.tid in s.tags
Join Syntax at dev.mysql.com
精彩评论