有我的桌子:
|category|
|--------|
|id_cat |
|name_cat|
填写表类别:
cat1 | BOX A
cat2 | BOX B
第二张表:
|display |
|----------|
|id_display|
|name_1 |
|name_2 |
表格显示填写:
dis1 | cat1 | cat2
然后我用这个QUERY加入两个表
$sql = "SELECT * FROM display d JOIN category c ON d.name_1=c.id_cat JOIN category c ON d.name_2=c.id_cat ORDER BY $id_display DESC";
$result = mysql_query($sql) or exit("Failed : ".$sql);
while($data = mysql_fetch_assoc($result)){
$id_display = $data['id_display'];
$name1 = $data['name_cat'];
$name2 = $data['name_cat'];
echo "
<div>
$name1<br/>
$name2<br/>
</div>
";
}
结果是:
BOX A
BOX A
如何显示这样的结果?
BOX A
BOX B
答案 0 :(得分:0)
$sql = "select d.name_1 as display1, d.name_2 as display2, c.name_cat as cat, c2.name_cat as cat2 FROM display d
INNER JOIN category c ON(d.name_1 = c.id_cat)
INNER JOIN category c2 ON(d.name_2 = c2.id_cat)
ORDER BY $id_display DESC"
$result = mysql_query($sql) or exit("Failed : ".$sql);
while($data = mysql_fetch_assoc($result)){
$id_display = $data['id_display'];
$name1 = $data['cat'];
$name2 = $data['cat2'];
echo "
<div>
$name1<br/>
$name2<br/>
</div>
";
}
查看联接中列和表的命名别名。