我有3张表
- 站点:ID,名称
- assoc: type, type_id, site_id
- 选项:ID, name
我想查询选择*网站,然后从assoc选择相关的选项,并从选项表中抓取名称。
代码我想缩短为一个查询:
$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)){
$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/>';
}
它被称为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'
问题:assoc表中没有assoc_id