Php 前三名客户的Mysql查询

Php 前三名客户的Mysql查询,php,mysql,Php,Mysql,我试图查询前三名客户。我有三张桌子: 客户表(客户ID,公司) 产品表(产品ID、产品名称、价格) 订单表(订单ID、日期、金额、客户ID) 目前,我得到了以下信息: Top 3 Customers CustomerID Company Cost of Product Ordered 10042 HP 80.00 10043 Acer 20.00 10044 Sony 40

我试图查询前三名客户。我有三张桌子:

  • 客户表(客户ID,公司)
  • 产品表(产品ID、产品名称、价格)
  • 订单表(订单ID、日期、金额、客户ID)
目前,我得到了以下信息:

 Top 3 Customers

CustomerID  Company         Cost of Product Ordered
10042       HP          80.00
10043       Acer            20.00
10044       Sony            40.00
我正在使用此代码显示:

$result = mysql_query($query);
$num=mysql_numrows($result);
echo "<table border='1'>
<tr>
<th>CustomerID</th>
<th>Company</th>
<th>Cost of Product Ordered</th>

</tr>";

while($row = mysql_fetch_array($result)) 
  {
  echo "<tr>";
  echo "<td>" . $row['CustomerID'] . "</td>";
  echo "<td>" . $row['Company'] . "</td>";
  echo "<td>" . $row['total_amount'] . "</td>";

  echo "</tr>";
  }
echo "</table>";
$result=mysql\u查询($query);
$num=mysql\u numrows($result);
回声“
客户编号
单位
订购产品的成本
";
while($row=mysql\u fetch\u数组($result))
{
回声“;
回显“$row['CustomerID']”;
回显“$行[“公司]”;
回显“$行[“总金额]”;
回声“;
}
回声“;

使用
求和
而不是
计数

SELECT 
    Customer.CustomerID,
    Customer.Company, 
    SUM(CustomerOrder.Amount) AS total_amount
FROM CustomerOrder
INNER JOIN Customer
ON Customer.CustomerID = CustomerOrder.CustomerID
GROUP BY Customer.CustomerID 
ORDER BY total_amount DESC
LIMIT 3

使用以下查询。。。我检查过了…它工作了

select ordr.CustomerID,cust.Company,(Sum(ordr.Amount)) as  'Cost of Products Ordered' from `order` ordr , customer cust where cust.CustomerID=ordr.CustomerID group by ordr.CustomerID order by Sum(ordr.Amount) desc limit 3

“目前我有一个mysql,但它没有按照我的要求显示。”你能告诉我查询结果并解释它与你想要的有什么不同吗?嗯。对我来说,“总计”和“计数”似乎不是一回事…@MarkByers我已经更新了我在问题中得到的内容。@user1501784:你的查询返回4列,但是你只得到了3列。您的输出似乎与您的查询不匹配。“但我发现了错误”。什么错误?另外,请不要在评论中发布代码。更新您的问题以包含回答您的问题所需的所有相关信息。我得到的错误是:警告:mysql_numrows()希望参数1是资源,布尔值在C:\xampp\。。。。在第57ok行,我添加了$result=mysql\u query($query)或die($query。“

”。.mysql\u error());并得到以下错误:函数db.SUM不存在。检查参考中的“函数名解析和解析”部分Manual@user1501784:您是否在
总和之后但在
)之前添加了空格?
$result = mysql_query($query);
$num=mysql_numrows($result);
echo "<table border='1'>
<tr>
<th>CustomerID</th>
<th>Company</th>
<th>Cost of Product Ordered</th>

</tr>";

while($row = mysql_fetch_array($result)) 
  {
  echo "<tr>";
  echo "<td>" . $row['CustomerID'] . "</td>";
  echo "<td>" . $row['Company'] . "</td>";
  echo "<td>" . $row['total_amount'] . "</td>";

  echo "</tr>";
  }
echo "</table>";
SELECT 
    Customer.CustomerID,
    Customer.Company, 
    SUM(CustomerOrder.Amount) AS total_amount
FROM CustomerOrder
INNER JOIN Customer
ON Customer.CustomerID = CustomerOrder.CustomerID
GROUP BY Customer.CustomerID 
ORDER BY total_amount DESC
LIMIT 3
select ordr.CustomerID,cust.Company,(Sum(ordr.Amount)) as  'Cost of Products Ordered' from `order` ordr , customer cust where cust.CustomerID=ordr.CustomerID group by ordr.CustomerID order by Sum(ordr.Amount) desc limit 3