我在MySQL中使用Codeigniter框架。我想显示总和(amtreceived)> 50000的记录。否则我想用作为总和(iman_sub_product_payment_details.amtreceived)作为值的值进行检查。是否有可能像这样进行检查。有人请帮助我。以下是我的查询
$sql='SELECT pay_date,sub_ins_name as name, sum(iman_sub_product_payment_details.amtreceived) as amount, sum(iman_sub_product_payment_details.refundedamt) as refamount,iman_sub_product_payment_details.subscriber_id as subid FROM iman_subscriber_insitution_details LEFT JOIN `iman_sub_product_payment_details` ON `iman_sub_product_payment_details`.`subscriber_id`=`iman_subscriber_insitution_details`.`sub_institute_id`';
if(!empty($data['fromdate'])){
$sql.=' where iman_sub_product_payment_details.pay_date >= date("'.$fromdate.'")';
}if(!empty($data['todate'])){
$sql.=' and iman_sub_product_payment_details.pay_date <= date("'.$todate.'")';
}if($data['comboval']!=0){
$sql.=' and iman_sub_product_payment_details.combostatus ="1"';
}if($data['from_amount']!=""){
$sql.=' and amtreceived >="'.$data['from_amount'].'"';
}
if($data['to_amount']!=""){
$sql.=' and amtreceived <="'.$data['to_amount'].'"';
}
if($data['currency']!=0){
$sql.=' and iman_sub_product_payment_details.cur_id ="'.$data['currency'].'"';
}
$sql.=' group by iman_subscriber_insitution_details.sub_institute_id';
$sql.=' UNION
SELECT pay_date,agentname as name,sum(iman_sub_product_payment_details.amtreceived) as amount,sum(iman_sub_product_payment_details.refundedamt) as refamount,iman_sub_product_payment_details.subscriber_id as subid FROM iman_agent_details
LEFT JOIN `iman_sub_product_payment_details` ON `iman_sub_product_payment_details`.`dealer_id`=`iman_agent_details`.`agentid` ';
if(!empty($data['fromdate'])){
$sql.=' where iman_sub_product_payment_details.pay_date >= date("'.$fromdate.'")';
}if(!empty($data['todate'])){
$sql.=' and iman_sub_product_payment_details.pay_date <= date("'.$todate.'")';
}if($data['comboval']!=0){
$sql.=' and iman_sub_product_payment_details.combostatus ="1"';
}if($data['from_amount']!=""){
$sql.=' and amtreceived >="'.$data['from_amount'].'"';
}
if($data['to_amount']!=""){
$sql.=' and amtreceived <="'.$data['to_amount'].'"';
}
if($data['currency']!=0){
$sql.=' and iman_sub_product_payment_details.cur_id ="'.$data['currency'].'"';
}
$sql.=' group by iman_agent_details.agentid';
$sql.=' order by name asc';
$query=$this->db->query($sql);
您需要的是GROUP BY之后的HAVING子句。如 :
HAVING SUM(amtreceived) > 5000
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句