这是我的表格和我目前的方法。查询太多,所以最有效的方法呢?
Table: contents
content_id
Table: taxonomy_terms
tt_id
type (tag or category)
name
Table: relationships
content_id
tt_id
伪代码:
mysql: query limit 20 contents from **contents**
php: for each content
mysql: query tags and categories from **relationships** with content_id
Number of queries: 1 + 20 = 21
答案 0 :(得分:1)
您可以先迭代第一个查询,然后执行类似
的操作$ids = array();
$contents = array();
while ($row = $res->fetch_assoc())
{
/* your existing code */
/* such as fill-up an array */
$contents[$row['content_id']] = $row;
$ids[] = $row['content_id'];
}
$sql = 'SELECT ... WHERE tt_id in('.implode(',', $ids).')';
/* fetch the tag/category results */
/* and update $contents with tag/category results */
有了这个,需要两个查询 还有一个额外的PHP循环,用于第一个查询中的20个内容