簡體   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