【发布时间】:2014-10-05 18:59:01
【问题描述】:
说,我在 MySQL 上有一个名为 tbl_order 的表,其结构如下(仅作说明):
然后我做了以下查询:
SELECT * FROM `tbl_order` WHERE `tbl_order`.`grade` = 'AAA' AND `tbl_order`.`order_confim_time` < NOW() ORDER BY `tbl_order`.`status` DESC, `tbl_order`.`order_confim_time` ASC
这样我们就可以得到以下结果:
-------------------------------------------------- -------------------------------------------------- ----- |编号 |等级 |类型 |订单日期 | order_confim_time |卷入 |音量输出 | vol_dev |状态 | |人工智能 |变量 |变量 |时间戳 |时间戳 |双 |双 |双 |变量 | -------------------------------------------------- -------------------------------------------------- ----- | 91 | AAA |输出 | 2014-09-12 09:06:00 |空 | 0 | 130000 | -2000 |通知 | | 93 | AAA |输出 | 2014-09-13 10:05:00 | 2014-09-12 14:00:00 | 0 | 115000 | -5000 |确认 | | 89 | AAA |输出 | 2014-09-12 09:04:00 | 2014-09-12 13:00:00 | 0 | 120000 | -5000 |确认 | | 87 | AAA |输出 | 2014-09-11 10:03:00 | 2014-09-11 14:00:00 | 0 | 145000 | -5000 |确认 | | 85 | AAA |输出 | 2014-09-11 09:02:00 | 2014-09-11 13:00:00 | 0 | 99000 | -1000 |确认 | | 83 | AAA |输入 | 2014-09-10 10:01:00 | 2014-09-10 14:00:00 | 998000 | 0 | -2000 |确认 | | 81 | AAA |输入 | 2014-09-10 09:00:00 | 2014-09-10 13:00:00 | 498000 | 0 | -2000 |确认 | -------------------------------------------------- -------------------------------------------------- -----我确实在我的模型Order 中编写了以下函数,以便使用递归获取上一行:
结果相当好,只尊重order_confirm_time 和NOT NULL 值。
The expected condition is something like this
如何在不需要依赖ORDER BY id DESC 的情况下获取上一行?目前我依赖ORDER BY order_confirm_time。
我正在使用 YII 1.1.16 分支,php 5.4 @ Windows 7 x64,MariaDB 5.5.38
【问题讨论】:
-
我不明白你的问题“我怎样才能得到 id = 93 的行而不需要依赖 ORDER BY id DESC”,你能只发布你想要得到的输出吗?
-
@brian-demilia,对不起,如果我的问题不清楚。目标/预期结果是我可以在不依赖
id字段的情况下获得前一行 ID。目前我正在使用ORDER BY order_confim_time,但只显示非空(order_confim_time 与值)数据。请看这张照片current condition。有2个圆圈,红色和绿色。我想做这样的expected condition。 -
@Brian DeMilia,对我的案子有什么想法吗?我已经简化了我的代码,但仍然只得到前一行。其余的仍然指向第一行。