简体   繁体   English

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

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

I have two different but related tables and I want to be able to edit/update the information that is retrieved and displayed in a Form. 我有两个不同但相关的表,我希望能够编辑/更新在表单中检索和显示的信息。 In plain english here's what I am trying to achieve: 用简单的英语来说,这就是我想要达到的目标:

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

But at the same time, I want to: 但同时,我想:

SELECT colum_A, colum_B, colum_C FROM table serialNumbers WHERE request_id = {$id} 从表序列号中选择colum_A,colum_B,colum_C,而request_id = {$ id}

What's the correct/best way to do this all at once? 一次完成所有操作的正确/最佳方法是什么?

Question Update: I have two files (1)my_request.php (which lists the details from the table requests), and (2) configuration.php (which is the form I will use to edit/update both table - mainly the serial numbers table). 问题更新:我有两个文件(1)my_request.php(列出了表请求的详细信息)和(2)configuration.php(这是我将用来编辑/更新两个表的形式-主要是序列号)表)。 How do I pass on the id from my_request.php to the configuration.php... so that I can apply the instruction you provided above 如何将id从my_request.php传递到configuration.php ...,以便我可以应用您在上面提供的说明

For my_request.php I have the following code. 对于my_request.php,我有以下代码。 I am able to see all the requests created by the logged in user: 我能够看到登录用户创建的所有请求:

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>';

What do I need to do on configuration.php so it understands which id to retrieve the info from? 我需要在configuration.php上执行什么操作,以便它了解从哪个ID检索信息? For the configuration.php I basically have the html form where I want to display the data. 对于configuration.php,我基本上具有要在其中显示数据的html表单。

You need a simple join: 您需要一个简单的联接:

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}

Depending of content of table 2 you can use LEFT OUTER JOIN in case you have records only in requests table. 如果仅在请求表中有记录,则可以根据表2的内容使用LEFT OUTER JOIN。 More info about MySQL Joins -> http://www.sitepoint.com/understanding-sql-joins-mysql-database/ 有关MySQL联接的更多信息-> http://www.sitepoint.com/understanding-sql-joins-mysql-database/

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

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