使用 MySQL 5.5.30,对于具有以下记录的表 purchase_order_product (MyISAM):
+----------+------------------+
| order_id | order_product_id |
+----------+------------------+
| 98 | 1 |
| 99 | 14 |
| 99 | 15 |
| 100 | 16 |
| 100 | 17 |
| 100 | 18 |
| 101 | 19 |
| 102 | 20 |
+----------+------------------+
当我从 MySQL Workbench 5.2.47 CE 运行以下查询时:
SELECT pop.order_id, pop.order_product_id,
@RUNNING:=IF(@PREVIOUS = pop.order_id, @RUNNING, 0) + 1 AS rownum,
@PREVIOUS:=pop.order_id as previd
FROM purchase_order_product pop
ORDER BY pop.order_id , pop.order_product_id ASC;
我得到以下输出:
+----------+------------------+--------+--------+
| order_id | order_product_id | rownum | previd |
+----------+------------------+--------+--------+
| 98 | 1 | 1 | 98 |
| 99 | 14 | 1 | 99 |
| 99 | 15 | 2 | 99 |
| 100 | 16 | 1 | 100 |
| 100 | 17 | 2 | 100 |
| 100 | 18 | 3 | 100 |
| 101 | 19 | 1 | 101 |
| 102 | 20 | 1 | 102 |
+----------+------------------+--------+--------+
我想要的目标是 rownum 的值,这很好,到目前为止,正如预期的那样......
但是,当我从我的 PHP 代码中或从 mysql 命令行使用相同的 db 用户对同一数据库运行查询时,我得到以下输出:
+----------+------------------+--------+--------+
| order_id | order_product_id | rownum | previd |
+----------+------------------+--------+--------+
| 98 | 1 | 1 | 98 |
| 99 | 14 | 1 | 99 |
| 99 | 15 | 1 | 99 |
| 100 | 16 | 1 | 100 |
| 100 | 17 | 1 | 100 |
| 100 | 18 | 1 | 100 |
| 101 | 19 | 1 | 101 |
| 102 | 20 | 1 | 102 |
+----------+------------------+--------+--------+
如您所见,rownum 始终为 1!
我已经广泛搜索这个令人困惑的问题的解决方案,但无济于事。有谁知道可能发生了什么?难道我做错了什么?