php - How to select previous record not depends on ID which is AutoIncrements in MySQL? -



php - How to select previous record not depends on ID which is AutoIncrements in MySQL? -

say, have table named tbl_order on mysql construction (just illustration):

--------------------------------------------------------------------------------------------------------- | 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 | ---------------------------------------------------------------------------------------------------------

then did next query:

select * `tbl_order` `tbl_order`.`grade` = 'aaa' , `tbl_order`.`order_confim_time` < now() order `tbl_order`.`status` desc, `tbl_order`.`order_confim_time` asc

so in next result:

--------------------------------------------------------------------------------------------------------- | 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 | ---------------------------------------------------------------------------------------------------------

i did write next function in model order in order previous row using recursive:

// made simplify code, still code work on 1st previous row. rest still not. public function getprevrow() { if (is_null($this->order_confim_time)) { $data = self::model()->find(array( 'condition' => '`t`.`grade`=:type_order , (`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)) homecoming 0; else homecoming 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)) homecoming 0; else homecoming floatval($data->prevrow) + floatval($data->vol_in) + floatval($data->vol_dev) - floatval($data->vol_out); } }

the result quite , respects order_confirm_time not null values.

current result in cgridview

the expected status this

how can previous row without need depends on order id desc? depends on order order_confirm_time.

i using yii 1.1.16-branch, php 5.4 @ windows 7 x64, mariadb 5.5.38

in pseudo code, can this:

initialize tempv_var initialize my_array loop records do conditions here before passing records my_array stored records in my_array[temp_var] increment temp_var

at end have my_array contents records index of temp_var. straight forwards solutions, can within sql query , adding logic on script.

php mysql sql yii

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -