使用MySQL关系表循环行

时间:2013-06-28 20:12:47

标签: php html mysql

我有三张桌子:

ID | Name | Price | etc.

面板

ID | Name

门-关系

door_id | panel_id

我想生成一个HTML / PHP表,每个门都有一行显示与之相关的面板:

门禁清单

Name | Price | Panel | etc.

我在编写MySQL查询时遇到了麻烦。我该如何从数据库中请求此信息?谢谢!

我尝试了什么:

<?php   
$catalog_query = mysqli_query($con,"SELECT * FROM door");


$panel_query = mysqli_query($con,"SELECT door.name as door, panel_profile.name as panel FROM door_relationship
INNER JOIN door
    ON door_relationship.doorid = door.door_id
INNER JOIN panel_profile 
    ON door_relationship.panel_id = panel_profile.panel_id
INNER JOIN door");


while($row = mysqli_fetch_array($catalog_query) and $row2 = mysqli_fetch_array($panel_query))
{
echo    '<tr>
            <td>' . $row['door'] . '</td>
            <td>' . $row['description'] . '</td>
            <td>' . $row['sqft_price'] . '</td>
            <td>' . $row['door_price'] . '</td>
            <td>' . $row2['panel'] . '</td>
            <td>' . $row['joint'] . '</td>
            <td>' . $row['collection'] . '</td>
            <td>' . $row['min_sqft'] . '</td>
            <td>' . $row['min_width'] . '</td>
            <td>' . $row['min_height'] . '</td>
            <td>' . $row['max_width'] . '</td>
            <td>' . $row['max_height'] . '</td>
        </tr>'
        ;
}
?>  

3 个答案:

答案 0 :(得分:1)

这可能会有所帮助(未经测试):

SELECT
  door.id AS door_id,
  door.name AS door_name,
  door.price AS door_price,
  door.etc AS door_etc,
  panel.id AS panel_id,
  panel.name AS panel_name
FROM door, panel
INNER JOIN door_relationship
  ON door_relationship.door_id = door_id
  AND door_relationship.panel_id = panel_id

答案 1 :(得分:1)

查询必须是:

select d.name doorName, p.name panelName, otherFields from Door d, Panel p, Door-Relationship r where p.id = r.panel_id and d.id = r.door_id group by d.id order by d.id asc

答案 2 :(得分:0)

SELECT d.Name, d.Price, p.Name, etc

FROM Door d, Panel p, Door-Relationship DR

WHERE d.id = dr.door_id and p.id = dr.panel_id;