简体   繁体   中英

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}

But at the same time, I want to:

SELECT colum_A, colum_B, colum_C FROM table serialNumbers WHERE 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). 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

For my_request.php I have the following code. 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? For the configuration.php I basically have the html form where I want to display the data.

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. More info about MySQL Joins -> http://www.sitepoint.com/understanding-sql-joins-mysql-database/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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