-1

我知道在阅读许多答案之前已经涵盖了这个问题,它们都告诉我如何编写 SQL,但没有告诉我如何打印结果。

我的困境是我有一个带有付款方式字段的订单表,看起来像这样

OrderID     OrderNumber     PaymentMethod
 1234         a1b2c3          PayPal
 1235         a1b2c4          Account
 1236         a1b2c5          Account
 1237         a1b2c6          PayPal
 1238         a1b2c7          Account
 1239         a1b2c8          PayPal
 1230         a1b2c9          PayPal

我需要打印出类似的东西

贝宝付款次数:4

账户支付次数:3

我试图使用这个答案

MySQL:计算不同值的出现次数

我有这个代码

SELECT PaymentMethod,COUNT(*) as Payment FROM tablename GROUP BY PaymentMethod ORDER BY Payment DESC;

但是我该如何打印出来呢?

4

2 回答 2

1
$returned = mysqli_query("SELECT PaymentMethod,COUNT(distinct PaymentMethod) as Payment FROM tablename GROUP BY PaymentMethod ORDER BY Payment DESC;");
while($data = mysqli_fetch_assoc($returned)){
    echo "Number of " . $data['PaymentMethod'] . ": " . $data[0] . "<br>";
}
于 2013-10-17T14:27:54.463 回答
0

您应该将数据表示留给 VIEW。

话虽如此,这就是您可以使用纯 MySQL 提取以这种方式格式化的数据的方法:

SELECT concat('Number of ', paymentmethod ,' payments: ', count(*)) RESULT FROM t
GROUP BY paymentmethod
ORDER BY paymentmethod DESC

这导致:

|                        RESULT |
|-------------------------------|
|  Number of PayPal payments: 4 |
| Number of Account payments: 3 |

在这里拉小提琴。

于 2013-10-17T14:38:39.747 回答