2010-12-04 3 views
1

ここに私のテーブルと私の現在の方法です。あまりにも多くのクエリ、これを行うための最も効率的な方法は?コンテンツのリストをクエリする効率的な方法、各コンテンツはタグとカテゴリのリストを取得する必要があります

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 

答えて

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 */ 

ような何かを、2つのクエリを必要とすることができます。
さらに、最初のクエリで取得した20のコンテンツのPHPループ

関連する問題