如何使用codeigniter计算来自数据库的重复记录的序列化购物车数量?

时间:2016-12-19 21:04:31

标签: php mysql codeigniter codeigniter-2

我有一个DB表,我用serialize存储了购物车数据。我希望count购物车qty包含重复行。我该如何解决?

假设我有两行,并且我希望从这些行计算qty2

a:3:{s:32:"3c59dc048e8850243be8079a5c74d079";a:6:{s:5:"rowid";s:32:"3c59dc048e8850243be8079a5c74d079";s:2:"id";s:2:"21";s:3:"qty";s:1:"6";s:5:"price";s:3:"250";s:4:"name";s:20:"Beef SIndhi Biriyane";s:8:"subtotal";i:1500;}s:32:"6ea9ab1baa0efb9e19094440c317e21b";a:6:{s:5:"rowid";s:32:"6ea9ab1baa0efb9e19094440c317e21b";s:2:"id";s:2:"29";s:3:"qty";s:1:"1";s:5:"price";s:2:"90";s:4:"name";s:14:"Egg Fried Rice";s:8:"subtotal";i:90;}s:32:"98f13708210194c475687be6106a3b84";a:6:{s:5:"rowid";s:32:"98f13708210194c475687be6106a3b84";s:2:"id";s:2:"20";s:3:"qty";s:1:"1";s:5:"price";s:3:"120";s:4:"name";s:19:"Hyderabadi Biriyane";s:8:"subtotal";i:120;}}

a:3:{s:32:"3c59dc048e8850243be8079a5c74d079";a:6:{s:5:"rowid";s:32:"3c59dc048e8850243be8079a5c74d079";s:2:"id";s:2:"21";s:3:"qty";s:1:"6";s:5:"price";s:3:"250";s:4:"name";s:20:"Beef SIndhi Biriyane";s:8:"subtotal";i:1500;}s:32:"6ea9ab1baa0efb9e19094440c317e21b";a:6:{s:5:"rowid";s:32:"6ea9ab1baa0efb9e19094440c317e21b";s:2:"id";s:2:"29";s:3:"qty";s:1:"1";s:5:"price";s:2:"90";s:4:"name";s:14:"Egg Fried Rice";s:8:"subtotal";i:90;}s:32:"98f13708210194c475687be6106a3b84";a:6:{s:5:"rowid";s:32:"98f13708210194c475687be6106a3b84";s:2:"id";s:2:"20";s:3:"qty";s:1:"1";s:5:"price";s:3:"120";s:4:"name";s:19:"Hyderabadi Biriyane";s:8:"subtotal";i:120;}}

1 个答案:

答案 0 :(得分:0)

这是我对这个问题的解决方案。

$this->db->select( "*, count(substring_index(substring_index(order_data,';',7),':',-1)) AS total", FALSE );         
$this->db->from( "order" );
$this->db->group_by( "order_data" );

$res = $this->db->get();

return ( $res->num_rows() > 0 ) ? $res->result() : false;