繁体   English   中英

PHP / MySql-从两个不同的表中检索数据的正确语法是什么?

[英]PHP/MySql - What is the correct syntax for retrieving data from two different tables?

我有两个不同但相关的表,我希望能够编辑/更新在表单中检索和显示的信息。 用简单的英语来说,这就是我想要达到的目标:

在表请求中选择colum_1,colum_2,colum_3,WHERE request_id = {$ id}

但同时,我想:

从表序列号中选择colum_A,colum_B,colum_C,而request_id = {$ id}

一次完成所有操作的正确/最佳方法是什么?

问题更新:我有两个文件(1)my_request.php(列出了表请求的详细信息)和(2)configuration.php(这是我将用来编辑/更新两个表的形式-主要是序列号)表)。 如何将id从my_request.php传递到configuration.php ...,以便我可以应用您在上面提供的说明

对于my_request.php,我有以下代码。 我能够看到登录用户创建的所有请求:

while ($row = mysql_fetch_assoc($results)) {

                        $orderdate = strtotime($row['sl_date']);
                        $fdate = strftime("%d/%m/%Y", $orderdate);
                        echo '<tr>';
                        echo '<td class="txtLeft">' . $row['request_date'] . '</td>';
                        echo '<td class="txtLeft">' . $row['client_name'] . '</td>';
                        echo '<td class="txtCenter">' . $row['client_country'] . '</td>';
                        echo '<td class="txtCenter">' . $row['opportunity_number'] . '</td>';
                        echo '<td class="txtCenter">' . $row['machine_quantity'] . '</td>';
                        echo '<td class="txtCenter">' . $row['severity'] . '</td>';
                        echo '<td class="txtCenter">' . $row['request_status'];
                        echo '</td>';
                         echo '<td class="txtCenter" id="task1">
                            <a href="configuraion.php?request_id=' . $row['request_id'] . '">
                            <img src="images/edit.png" width="16" height="16" title="edit sale" style="margin:1px;"/>
                            </a> 

                            <a href="' . $row['sales_connect'] . '" onClick="return confirm(\''. $LANG['alert_sale_del'] .'\');">
                            <img src="images/s.png" width="16" height="16" title="Sales Connect" style="margin:1px;"/>
                            </a>
                            </td>'; 
                        echo '</tr>';

我需要在configuration.php上执行什么操作,以便它了解从哪个ID检索信息? 对于configuration.php,我基本上具有要在其中显示数据的html表单。

您需要一个简单的联接:

SELECT t1.colum_1, t1.colum_2, t1.colum_3, t2.colum_A, t2.colum_B, t2.colum_C
FROM requests t1
LEFT JOIN serialNumbers t2
ON t1.request_id=t2.request_id
WHERE t1.request_id={$id}

如果仅在请求表中有记录,则可以根据表2的内容使用LEFT OUTER JOIN。 有关MySQL联接的更多信息-> http://www.sitepoint.com/understanding-sql-joins-mysql-database/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM