如何在Codeigniter中使用mysql substring_index计算JSON格式数据的总和

时间:2018-08-31 06:19:52

标签: php mysql codeigniter phpmyadmin

4月,5月,6月,我每个员工都有3行月薪详细信息。 Payslip_earnings列以

之类的JSON格式保存了详细信息
{"basicpay":40000,"hra":16000,..etc }

使用

在phpmyadmin中分别执行查询时,我可以获取json数据变量的总和。
sum(substring_index ( substring_index (payslip_earnings,'{"basicpay":',-1),'," ',1) ) as basic.

面对空白页,在codeiginter模型页中选择基本内部时发生错误/语法错误

$query = $this->db->query("SELECT payslip.payslip_empcode, 
         sum(substring_index ( substring_index (payslip_earnings,'{"basicpay":',-1),'," ',1) ) as basic
         FROM payslip LEFT OUTER JOIN employee_official_master ON
         payslip.payslip_empcode=employee_official_master.employee_code 
         WHERE payslip.payslip_fyear = '2018' and  
         payslip.payslip_empcode IN ('MAR015','MAR010') AND 
         ( payslip_month = 'April' OR payslip_month = 'May' OR payslip_month = 'June') 
GROUP BY payslip.payslip_empcode "
           );

如何在codeigniter模型页面中实现相同?

1 个答案:

答案 0 :(得分:1)

Try this One..
$colname  = '{"basicpay":' ;            
         $seperator = ',';  
         $query = $this->db->query("SELECT payslip.payslip_empcode, 
         sum(substring_index (substring_index (payslip.payslip_earnings,'".$colname."',-1),'".$seperator."',1) ) as basicpay
         FROM payslip LEFT OUTER JOIN employee_official_master ON
         payslip.payslip_empcode=employee_official_master.employee_code 
         WHERE payslip.payslip_fyear = '2018' and  
         payslip.payslip_empcode IN ('MAR015','MAR010') AND 
         ( payslip_month = 'April' OR payslip_month = 'May' OR payslip_month = 'June') 
GROUP BY payslip.payslip_empcode "
           );