为每个客户mysql返回一行

时间:2014-07-21 10:13:03

标签: mysql

我正在试图找出每个客户如何打印一行。现在我跑的时候

SELECT email_address, sum(quantity * item_price), sum(discount_amount * quantity)
FROM customers JOIN order_items
ORDER BY sum(quantity * item_price)

我只收到第一个客户。就价格而言,一切都是正确的,但它只打印出第一个客户。我需要表中的所有客户。有什么建议?我需要加入任何表吗?它似乎没有加入表格。哦,我还需要它根据item_price按降序排列。任何帮助深表感谢。提前致谢

2 个答案:

答案 0 :(得分:1)

试试这样:

SELECT email_address, sum(quantity * item_price), sum(discount_amount * quantity)
FROM customers JOIN order_items on customers.id= order_items.id
GROUP BY (email_address)
ORDER BY sum(quantity * item_price)

答案 1 :(得分:0)

你可以使用循环

在数组中获取它
$query ="SELECT email_address, sum(quantity * item_price), sum(discount_amount * quantity)
         FROM customers JOIN order_items
ORDER BY sum(quantity * item_price)";

$records=$mysql_query($link,$query);

while($row=mysql_fetch_array($ecord))

{

print_r($row);

}