选择加入两个表PHP的查询

时间:2014-01-20 23:52:17

标签: php mysql select

我已将一个脚本放在一起,打印出“ sales_list ”表中的所有行,但只显示与登录用户匹配的“ users_sales_guild_id ”的行。这很好。

我要做的是打印所有行,但从“accessories_orders”表中检索匹配的 sales_id ,然后输入“accessories_orders_total” 并随查询提供状态,因此如果登录的人员“user_sales_guild_id”“1234”。

+--------+---------------+-------------------+----------+
| Model  | Customer Name | Accessories Total | Status   | 
+--------+---------------+-------------------+----------+
| Nissan | Malcom Smith  |                   | Add      |
| Ford   | Jane Smith    | 200.00            | Pending  | 
+------------------------+-------------------+----------+

因此,如果“accessories_orders”表中有匹配的行,那么它会打印“已发货”“accessories_orders_total”数据。如果没有匹配的行,那么它将显示一个“添加”链接,该链接指向add_accessories_sales.php。

我收到错误消息“Undefined index:sales_model”以及第一个查询中的其他所有内容,是否有人可以指出我哪里出错?

“sales_list”表格

+--------------------------------------------------------------------------------------------------------------------------+
| sales_list                                                                                                               |
+------+--------------------------+--------------------------+------------------------+-------------+----------------------+
| sales_id | users_sales_guild_id | sales_customer_firstname | sales_customer_surname | sales_model |  sales_entry_date    |
+----------+----------------------+--------------------------+------------------------+-------------+----------------------+
| 1        | 1234                 | Jane                     | Smith                  | Ford        | 2013-12-02 12:00:00  |
| 2        | 5678                 | John                     | Chan                   | Mazda       | 2013-12-03 12:00:00  |
| 3        | 5678                 | Kevin                    | Chan                   | Fiat        | 2013-12-04 12:00:00  |
| 4        | 1234                 | Malcom                   | Smith                  | Nissan      | 2013-12-05 12:00:00  |
+----------+----------------------+--------------------------+------------------------+-------------+----------------------+

“accessories_orders”表

+-------------------------------------------------------------------------------------------------------------------------+
| accessories_orders                                                                                                      |
+-----------------------+----------------------+----------+--------------------------+-------------------------+----------+
| accessories_orders_id | users_sales_guild_id | sales_id | accessories_orders_total | accessories_orders_date | shipped  |  
+-----------------------+----------------------+----------+--------------------------+-------------------------+----------+
| 1                     | 1234                 | 1        | 200.00                   | 2013-12-02 12:00:00     | Pending  |
| 2                     | 5678                 | 2        | 350.00                   | 2013-12-03 12:00:00     | Pending  | 
| 3                     | 5678                 | 3        | 100.00                   | 2013-12-03 12:00:00     | Pending  |
+-----------------------+----------------------+----------+--------------------------+-------------------------+----------+

已编辑及更新的代码

<?php
require_once ('database.php'); // Connect to the database.

$query = " SELECT sl.sales_model, sl.sales_customer_firstname, sl.sales_customer_surname, ao.accessories_orders_total, ao.shipped, 
       COALESCE(ao.shipped)
       FROM sales_list sl
       LEFT JOIN accessories_orders ao ON(ao.sales_id = sl.sales_id)
       WHERE sl.users_sales_guild_id ='".$_SESSION['users_sales_guild_id']."' 
       ORDER BY 
     ".$order_by." LIMIT ".$start.", ".$display;

$result = @mysql_query ($query); // Run the query.

echo '<table width="610" cellspacing="1" cellpadding="5" style="font-size:11px;">
<tr>
<td align="center">Model </td>
<td align="center">Customer Name</td>
<td align="center">Accessories Total</td>
<td align="center">Status</td></tr>';

$bg = '#ffffff'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$status = $row['shipped'];

$bg = ($bg=='#e1e3e6' ? '#cdcdcf' : '#e1e3e6'); // Switch the background color.   
echo '<tr bgcolor="' . $bg . '">';
echo  '<td align="center">' . $row['sl.sales_model'] . '</td>';
echo  '<td align="center">' . $row['sl.sales_customer_firstname'] . ' ' . $row['sl.sales_customer_surname'] . '</td>';
echo  '<td align="center">$' . $row['acc.accessories_orders_total'] . '</td>';

$str = '<td align="center">';
if($status == 'Pending') {
 $str .=' Pending</td></tr>';
}
else {
 $str .='<strong><a href="add_accessories_sales.php?sid=' . $row['sl.sales_id'] . '">Add</a></strong></td></tr>'; 
}   
echo $str;
}

echo '</table>';
mysql_free_result ($result); // Free up the resources.
mysql_close();  //Close the database connection.
?>

2 个答案:

答案 0 :(得分:1)

您的查询需要更像这样:

SELECT sl.sales_model, sl.sales_customer_firstname, sl.sales_customer_surname, ao.accessories_orders_total, COALESCE(ao.shipped, 'Add') status
FROM sales_list sl
LEFT JOIN accessories_orders ao ON(ao.sales_id = sl.sales_id)
WHERE sl.users_sales_guild_id = 1234;

LEFT JOIN是关键所在。即使sales_list中没有相应的条目,它也允许使用accessories_orders中的数据返回行。

请参阅此小提琴,了解一个工作示例:http://sqlfiddle.com/#!2/f7d3d/4

正如其他人已经说过的那样,您应该使用mysqli函数集而不是mysql函数集。

答案 1 :(得分:0)

首先,您不应该包含表名。例如:

//wrong
echo  '<td align="center">' . $row['sl.sales_model'] . '</td>';

//correct
echo  '<td align="center">' . $row['sales_model'] . '</td>';

其次,除非使用过时的PHP版本,否则不应使用mysql。切换到mysqliPDO

第三,您没有错误处理来告诉您查询是否成功。实际上,您与错误处理相反,因为您尝试使用@运算符来抑制错误。相反,你应该做这样的事情:

//assuming you switch to mysqli
mysqli_query("SELECT acc.accessories_orders_id, acc.users_id, acc.sales_id, 
      acc.accessories_orders_total, acc.accessories_orders_date, acc.shipped,
      acc.timestamp, sl.sales_id
      FROM accessories_orders AS acc, sales_list AS sl
      WHERE acc.sales_id = sl.sales_id");
if(mysqli_error()) {
    throw new Exception(mysqli_error(), mysqli_errno());
}

如果你这样做,你的mysql错误将被记录在你的php错误的同一个地方。然后,您将能够判断您的任何问题是否来自查询格式错误。