如何将两个不同的mysql查询的结果合并到一个表中

时间:2016-08-21 19:28:59

标签: php html mysql

请我有两张桌子' CUSTOMER'和'受益人'在我的数据库中,我希望将他们的一些记录调出来,放在一个表中。 ' CUSTOMER' table有' profile_picture'专栏和表格' BENEFICIARY'有' receiver_name'和' receiver_id'列,最后两者分别具有Id和sender_id列的共同点,这显然是客户的登录ID。

问题是,我完全不知道如何将两个记录显示在我希望在客户仪表板上的一个表中,我希望用户将他的两个受益人命名为他们的个人资料图片表

以下是我的两个不同查询:

FOR BENEFICIARY!

<php
include '_inc/dbconn.php';
$sender_id=$_SESSION["login_id"];
$sql="SELECT * FROM beneficiary WHERE sender_id='$sender_id' AND status='ACTIVE' ";
                $result=  mysql_query($sql) or die(mysql_error());
                while($rws=  mysql_fetch_array($result)){

                    .$rws[3]. //receiver_id
                    .$rws[4]. //receiver_name
                }
?>

FOR CUSTOMER!

<php
include '_inc/dbconn.php';
$sender_id=$_SESSION["login_id"];
$sql1="SELECT * FROM customer WHERE id='$sender_id' ";
                $result1=  mysql_query($sql1) or die(mysql_error());
                while($rows=  mysql_fetch_array($result1)){

                    .$rows[14]. //profile_pictures  
                }
?>

要获得的HTML表格!

<tr>
                                                                    <td class="center">.$rows[14].</td>
                                                                    <td><span class="text-small block text-light">.$rws[3].</span><span class="text-large">.$rws[4].</span><a href="#" class="btn"><i class="fa fa-pencil"></i></a></td>
                                                                    <td class="center">
                                                                    <div>
                                                                        <div class="btn-group">
                                                                            <a class="btn btn-transparent-grey dropdown-toggle btn-sm" data-toggle="dropdown" href="#">
                                                                                <i class="fa fa-cog"></i> <span class="caret"></span>
                                                                            </a>
                                                                            <ul role="menu" class="dropdown-menu dropdown-dark pull-right">
                                                                                <li role="presentation">
                                                                                    <a role="menuitem" tabindex="-1" href="#">
                                                                                        <i class="fa fa-edit"></i> Edit
                                                                                    </a>
                                                                                </li>
                                                                                <li role="presentation">
                                                                                    <a role="menuitem" tabindex="-1" href="#">
                                                                                        <i class="fa fa-share"></i> Share
                                                                                    </a>
                                                                                </li>
                                                                                <li role="presentation">
                                                                                    <a role="menuitem" tabindex="-1" href="#">
                                                                                        <i class="fa fa-times"></i> Remove
                                                                                    </a>
                                                                                </li>
                                                                            </ul>
                                                                        </div>
                                                                    </div></td>
                                                                </tr>

2 个答案:

答案 0 :(得分:1)

在客户表

中使用 JOIN 查询
SELECT beneficiary.receiver_name,customer.profile_picture
FROM CUSTOMER
INNER JOIN BENEFICIARY
ON BENEFICIARY.sender_id=CUSTOMER.sender_id;

答案 1 :(得分:0)

加入表格:

SELECT *
FROM beneficiary b
JOIN customer c ON c.id = b.sender_id
WHERE b.sender_id='$sender_id' AND b.status='ACTIVE'

完整代码:

<?php
    include '_inc/dbconn.php';
    $sender_id=$_SESSION["login_id"];
    $sql="SELECT *
        FROM beneficiary b
        JOIN customer c ON c.id = b.sender_id
        WHERE b.sender_id='$sender_id' AND b.status='ACTIVE'";
    $result = mysql_query($sql) or die(mysql_error());
    while($rws=  mysql_fetch_assoc($result)){
        echo '
        <tr>
            <td class="center">'.$rows['profile_pictures'].'</td>
            <td><span class="text-small block text-light">'.$rws['receiver_id'].'</span><span class="text-large">'.$rws['receiver_name'].'</span><a href="#" class="btn"><i class="fa fa-pencil"></i></a></td>
            <td class="center">
                <div>
                    <div class="btn-group">
                        <a class="btn btn-transparent-grey dropdown-toggle btn-sm" data-toggle="dropdown" href="#">
                            <i class="fa fa-cog"></i> <span class="caret"></span>
                        </a>
                        <ul role="menu" class="dropdown-menu dropdown-dark pull-right">
                            <li role="presentation">
                                <a role="menuitem" tabindex="-1" href="#">
                                    <i class="fa fa-edit"></i> Edit
                                </a>
                            </li>
                            <li role="presentation">
                                <a role="menuitem" tabindex="-1" href="#">
                                    <i class="fa fa-share"></i> Share
                                </a>
                            </li>
                            <li role="presentation">
                                <a role="menuitem" tabindex="-1" href="#">
                                    <i class="fa fa-times"></i> Remove
                                </a>
                            </li>
                        </ul>
                    </div>
                </div>
            </td>
        </tr>';
    }
?>