运行以使用PHP选择和分组结果

时间:2013-05-13 11:49:08

标签: php mysql

我正在尝试从主表中显示一行数据(父项详细信息),然后在每个行下显示另一个表中的子项名称。我已经使用以下代码:

$sql="select distinct members_main.first_name as main_firstname,
    members_main.first_name as main_firstname,
    members_main.last_name as main_lastname,
    members_main.address_1 as main_address_1,
    members_main.address_2 as main_address_2,
    members_main.address_3 as main_address_3,
    members_main.address_4 as main_address_4,
    members_main.post_code as main_post_code,
    members_main.home_tel as main_home_tel,
    members_main.mobile as main_mobile,
    members_main.home_email as main_home_email
    from members_main, members_family where members_main.contact_id=members_family.contact_id";

$sql2="select members_family.first_name as fam_firstname,
    members_family.last_name as fam_lastname
    from members_family, members_main 
    where members_main.contact_id=members_family.contact_id";

$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){

echo '<table width="100%"  cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
<th align="left" valign="top">Address 1</th>
<th align="left" valign="top">Address 2</th>
<th align="left" valign="top">Address 3</th>
<th align="left" valign="top">Address 4</th>
<th align="left" valign="top">Post Code</th>
<th align="left" valign="top">Home Tel</th>
<th align="left" valign="top">Mobile</th>
<th align="left" valign="top">Email</th>
</tr>';

echo '<tr>
<td valign="top">'.$rows['main_firstname'].'</td>
<td valign="top">'.$rows['main_lastname'].'</td>
<td valign="top">'.$rows['main_address_1'].'</td>
<td valign="top">'.$rows['main_address_2'].'</td>
<td valign="top">'.$rows['main_address_3'].'</td>
<td valign="top">'.$rows['main_address_4'].'</td>
<td valign="top">'.$rows['main_post_code'].'</td>
<td valign="top">'.$rows['main_home_tel'].'</td>
<td valign="top">'.$rows['main_mobile'].'</td>
<td valign="top">'.$rows['main_home_email'].'</td>
</tr>';

$result2=mysql_query($sql2);

echo '<table width="100%"  cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
</tr>';

while($rows=mysql_fetch_array($result2)){

echo '<tr>
<td valign="top">'.$rows['fam_firstname'].'</td>
<td valign="top">'.$rows['fam_lastname'].'</td>
</tr>';

}

}

echo "</table>";

我从members_main的每条记录中获取members_family表中的所有行。 members_family中可以有多个记录,这些记录将与members_main中的一个记录相关联。基本上,members_main(parent)可以在members_main中有多个子项。

2 个答案:

答案 0 :(得分:2)

我认为您只需要从第一个表中检索members_main.contact_id,以便稍后在第二个while循环中使用。然后,您必须在第一个查询while loop内移动第二个查询,以便代码看起来像这样:

$sql="select distinct 
members_main.contact_id as ID,
members_main.first_name as main_firstname,
members_main.first_name as main_firstname,
members_main.last_name as main_lastname,
members_main.address_1 as main_address_1,
members_main.address_2 as main_address_2,
members_main.address_3 as main_address_3,
members_main.address_4 as main_address_4,
members_main.post_code as main_post_code,
members_main.home_tel as main_home_tel,
members_main.mobile as main_mobile,
members_main.home_email as main_home_email
from members_main, members_family where members_main.contact_id=members_family.contact_id";

$result = mysql_query($sql);
while($rows=mysql_fetch_array($result)) {

//script goes on with table printing untill
<td valign="top">'.$rows['main_home_email'].'</td>
</tr>';

现在是查询的时候了

$sql2="select members_family.first_name as fam_firstname,
members_family.last_name as fam_lastname
from members_family, members_main 
where members_main.contact_id=members_family.contact_id and members_main.contact_id = '".$rows['ID']."'";

如您所见,我添加WHERE条件以限制从第一个查询中检索到的指定ID的结果。现在您可以打印表格的其余部分。哦,顺便说一下你关闭表应该看起来像那样

echo "</table></td></tr></table>";

这是因为你打开了两张桌子。

然后我希望您记住mysql_*函数已被弃用,因此我建议您切换为mysqliPDO

答案 1 :(得分:0)

我对你的第二个问题感到困惑。我想你必须使用来自第一个查询(members_main)的contact_id构建你的第二个查询。我以这种方式假设,我编码如下。可能会帮助你

        <?php
        $sql = "select distinct members_main.first_name as main_firstname,
            members_main.first_name as main_firstname,
            members_main.last_name as main_lastname,
            members_main.address_1 as main_address_1,
            members_main.address_2 as main_address_2,
            members_main.address_3 as main_address_3,
            members_main.address_4 as main_address_4,
            members_main.post_code as main_post_code,
            members_main.home_tel as main_home_tel,
            members_main.mobile as main_mobile,
            members_main.home_email as main_home_email
            from members_main, members_family where members_main.contact_id=members_family.contact_id";
        $result = mysql_query($sql);
        ?>
        <table width="100%"  cellpadding="3" cellspacing="0">
            <tr>
                <th align="left" valign="top">First Name</th>
                <th align="left" valign="top">Last Name</th>
                <th align="left" valign="top">Address 1</th>
                <th align="left" valign="top">Address 2</th>
                <th align="left" valign="top">Address 3</th>
                <th align="left" valign="top">Address 4</th>
                <th align="left" valign="top">Post Code</th>
                <th align="left" valign="top">Home Tel</th>
                <th align="left" valign="top">Mobile</th>
                <th align="left" valign="top">Email</th>
            </tr>
            <?php
            while ($rows = mysql_fetch_array($result))
            {
                $conId = $rows['contact_id']; //Which is comming from first Query
                $sql2 = "select members_family.first_name as fam_firstname,
            members_family.last_name as fam_lastname
            from members_family, members_main 
            where members_family.contact_id=$conId";

                $result2 = mysql_query($sql2);
                ?>
                <tr>
                    <td valign="top"><?= $rows['main_firstname']; ?></td>
                    <td valign="top"><?= $rows['main_lastname']; ?></td>
                    <td valign="top"><?= $rows['main_address_1']; ?></td>
                    <td valign="top"><?= $rows['main_address_2']; ?></td>
                    <td valign="top"><?= $rows['main_address_3']; ?></td>
                    <td valign="top"><?= $rows['main_address_4']; ?></td>
                    <td valign="top"><?= $rows['main_post_code']; ?></td>
                    <td valign="top"><?= $rows['main_home_tel']; ?></td>
                    <td valign="top"><?= $rows['main_mobile']; ?></td>
                    <td valign="top"><?= $rows['main_home_email']; ?></td>
                </tr>    

                <tr>
                    <td colspan="10">
                        <table width="100%"  cellpadding="3" cellspacing="0">
                            <tr>
                                <th align="left" valign="top">First Name</th>
                                <th align="left" valign="top">Last Name</th>
                            </tr>
                            <?php
                            while ($rowsFamily = mysql_fetch_array($result2))
                            {
                                ?>
                                <tr>
                                    <td valign="top"><?= $rowsFamily['fam_firstname']; ?></td>
                                    <td valign="top"><?= $rowsFamily['fam_lastname']; ?></td>
                                </tr>
                                <?php
                            }
                            ?>
                        </table>
                    </td>
                </tr>
                <?php
            }
            ?>
        </table>