0

我有一个看起来像的表:

Client_ID | Order_ID | 
10        | 1        | 
10        | 2        |
10        | 3        |
14        | 6        |
14        | 7        |
14        | 9        |

在 PHP 中是否有一种优雅的方法可以打印出这个结果集,将 client_id 组作为证据,而不是创建一个循环来记住以前的 Client_ID 并验证它是否发生了变化。

客户 10:

  • 订单 1
  • 订单 2
  • 订单 3

客户 14:

  • 订单 6
  • 订单 7
  • 订单 9
4

3 回答 3

2

SQL 所能做的就是捕获一系列与一行之间没有任何关系的行。由 PHP 对这些结果施加一些结构,并可能像您所设想的那样使用循环:

$last_client_id = null;
while($row = mysql_fetch_assoc($results)) {
    if ($row['client_id'] != $last_client_id) {
        // Do whatever it is you do for each new client
    }
    // Do whatever it is you do for each order
    $last_client_id = $row['client_id'];
}
于 2009-09-07T20:52:36.847 回答
1

看看 GROUP_CONCAT 函数: http ://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

编辑:我想我错误地假设你从数据库中获取它:/

于 2009-09-07T20:54:38.413 回答
1

短:没有。

Long:之前的迭代怎么会知道一个新的id来了?这里必须有某种案件处理,没有它是不可能的。

于 2009-09-07T20:55:16.820 回答