1
說,我有表上的MySQL名爲tbl_order
像這樣(只圖)結構:如何選擇以前的記錄不取決於在MySQL中的AutoIncrements ID?
--------------------------------------------------------------------------------------------------------- | id | grade | type | order_date | order_confim_time | vol_in | vol_out | vol_dev | status | | AI | VARCH | VARCH | TIMESTAMP | TIMESTAMP | DOUBLE | DOUBLE | DOUBLE | VARCH | --------------------------------------------------------------------------------------------------------- | 81 | AAA | IN | 2014-09-10 09:00:00 | 2014-09-10 13:00:00 | 498000 | 0 | -2000 | CONFIRM | | 83 | AAA | IN | 2014-09-10 10:01:00 | 2014-09-10 14:00:00 | 998000 | 0 | -2000 | CONFIRM | | 85 | AAA | OUT | 2014-09-11 09:02:00 | 2014-09-11 13:00:00 | 0 | 99000 | -1000 | CONFIRM | | 87 | AAA | OUT | 2014-09-11 10:03:00 | 2014-09-11 14:00:00 | 0 | 145000 | -5000 | CONFIRM | | 89 | AAA | OUT | 2014-09-12 09:04:00 | 2014-09-12 13:00:00 | 0 | 120000 | -5000 | CONFIRM | | 91 | AAA | OUT | 2014-09-12 09:06:00 | NULL | 0 | 130000 | -2000 | NOTCONF | | 93 | AAA | OUT | 2014-09-13 10:05:00 | 2014-09-12 14:00:00 | 0 | 115000 | -5000 | CONFIRM | ---------------------------------------------------------------------------------------------------------
然後我做了以下查詢:
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
所以,在我們可以得到以下結果:
--------------------------------------------------------------------------------------------------------- | id | grade | type | order_date | order_confim_time | vol_in | vol_out | vol_dev | status | | AI | VARCH | VARCH | TIMESTAMP | TIMESTAMP | DOUBLE | DOUBLE | DOUBLE | VARCH | --------------------------------------------------------------------------------------------------------- | 91 | AAA | OUT | 2014-09-12 09:06:00 | NULL | 0 | 130000 | -2000 | NOTCONF | | 93 | AAA | OUT | 2014-09-13 10:05:00 | 2014-09-12 14:00:00 | 0 | 115000 | -5000 | CONFIRM | | 89 | AAA | OUT | 2014-09-12 09:04:00 | 2014-09-12 13:00:00 | 0 | 120000 | -5000 | CONFIRM | | 87 | AAA | OUT | 2014-09-11 10:03:00 | 2014-09-11 14:00:00 | 0 | 145000 | -5000 | CONFIRM | | 85 | AAA | OUT | 2014-09-11 09:02:00 | 2014-09-11 13:00:00 | 0 | 99000 | -1000 | CONFIRM | | 83 | AAA | IN | 2014-09-10 10:01:00 | 2014-09-10 14:00:00 | 998000 | 0 | -2000 | CONFIRM | | 81 | AAA | IN | 2014-09-10 09:00:00 | 2014-09-10 13:00:00 | 498000 | 0 | -2000 | CONFIRM | ---------------------------------------------------------------------------------------------------------
我爲了寫下面的函數在我的模型Order
用遞歸以獲得上一行:
// i made simplify code, but still the code only work on 1st previous row. the rest still not. public function getPrevRow() { if (is_null($this->order_confim_time)) { $data = self::model()->find(array( 'condition' => '`t`.`grade`=:type_order AND (`t`.`order_date` '`t`.`status` DESC, `t`.`order_confim_time` DESC, `t`.`id` DESC', 'limit' => 1, 'params' => array('type_order' => $this->grade, ':current_date_confirm' => $this->order_confim_time), )); if (is_null($data)) return 0; else return floatval($data->PrevRow) + floatval($data->vol_in) + floatval($data->vol_dev) - floatval($data->vol_out); } else { $data = self::model()->find(array( 'condition' => '`t`.`order_confim_time` '`t`.`status` DESC, `t`.`id` DESC', 'limit' => 1, 'params' => array(':current_date_confirm' => $this->order_confim_time, 'type_order' => $this->grade), )); if (is_null($data)) return 0; else return floatval($data->PrevRow) + floatval($data->vol_in) + floatval($data->vol_dev) - floatval($data->vol_out); } }
結果相當不錯,只有尊重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的64位,MariaDB的38年5月5日
我不明白你的問題「我怎樣才能得到與ID = 93行無需取決於ORDER BY ID DESC」,你可以只發布你想要得到的輸出? – 2014-10-05 19:05:02
@ brian-demilia,對不起,如果我的問題不清楚。目標/預期的結果是我可以得到以前的行ID而不依賴於'id'字段。目前我使用'ORDER BY order_confim_time',但只顯示不爲空(帶有值的order_confim_time)數據。 請看看這張照片[當前情況](http://imgbox.com/SKf73tAy)。有2個圓圈,紅色和綠色。我想做出這樣的[預期條件](http://imgbox.com/ocnyOCha)。 – agungandika 2014-10-05 19:41:07
@Brian德米莉亞,對我的案件的任何想法?我已經簡化了我的代碼,但仍然只有第一行。其餘的仍然指向前一行。 – agungandika 2014-10-05 19:59:22