开发者

grab associated options from another table and merge with current results

I have 3 tables

  • sites: ID, name
  • assoc: type, type_id, site_id
  • opions: ID, name

I would like a query to select * sites then select the associated options from assoc and grab the names from the options table.

Code I would like to shorten to one query:

     $getsites = mysql_query("SELECT * FROM sites")or die(mysql_error());
     while($row = mysql_fetch_array($getsites)){
     echo $row['name'];
     $getassoc = mysql_query("SELECT * FROM assoc WHERE type='options' AND site_id = '$row[ID]'")or die(mysql_error());
      echo'<ul>';
      while($subrow = mysql_fetch_array($getassoc)){
      开发者_Python百科$getoption = mysql_query("SELECT * FROM options WHERE ID = '$subrow[assoc_id]'")or die(mysql_error());
      $option = mysql_fetch_assoc($getoption);
      echo '<li>'.$option['name'].'</li>';
   }
   echo'</ul><br/>';

   }


It is called a join:

select options.name
from sites
inner join assoc on sites.id = assoc.site_id
inner join options on options.id = assoc.assoc_id
where assoc.type = 'options'

Problem: There is no assoc_id in the assoc table

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜