我需要在Codeigniter中的连接查询中使用if语句。 我试试这个查询...
$partners = $this->db->select("h.partner_id,hd.id as detail_id,v.*, hd.handshake_id ,sum(if(hd.result = '0',1, 0)) AS not_tested ,sum(if(hd.result = '1',1, 0)) AS positive,sum(if(hd.result = '2',1, 0)) AS negative", FALSE)
->from("handshake h")
->join("visitor v", 'IF(h.visitor_id = "'.$visitor_data['id'].'", h.partner_id = v.id , h.visitor_id = v.id ) ', 'left',FALSE)
->join("handshake_detail hd", ' hd.handshake_id = h.id AND hd.visitor_id <> "'.$visitor_data["id"].'"', 'left',FALSE)
->where("h.visitor_id", $visitor_data['id'])
->or_where("h.partner_id", $visitor_data['id'])
->group_by("h.partner_id")
->get()->result_array();
如果我们在MySQL中编写上述查询,那么它可以工作,但是当我尝试使用Codeigniter语法时会产生这样的错误......
功能trustcrowd_1.0.IF不存在。检查'功能名称 参考手册中的解析和解决方案部分
所以请帮助找出Codeigniter查询的正确语法。
答案 0 :(得分:0)
我的工作查询是......
$this->db->set_dbprefix('');
$partners = $this->db->select("hd.visitor_id partner, h.id handshake, h.utp intro_date, v.name, v.email, v.birth_date, v.location, v.gender, v.image, v.test_date, v.clinic_place, SUM( IF( hd.result = '0', 1, 0 ) ) AS not_tested, SUM( IF( hd.result = '1', 1, 0 ) ) AS positive, SUM( IF( hd.result = '2', 1, 0 ) ) AS negative from tc_handshake h left join tc_visitor v on IF(h.visitor_id = '".$visitor_data['id']."', h.partner_id = v.id , h.visitor_id = v.id )", FALSE)
->join("tc_handshake_detail hd", 'h.id=hd.handshake_id AND hd.visitor_id <> "'.$visitor_data["id"].'"', 'left',FALSE)
->where("h.visitor_id", $visitor_data['id'])
->or_where("h.partner_id", $visitor_data['id'])
->group_by("h.partner_id,h.utp")
->order_by("h.id", "desc")
->limit($total_dis_record, $page)
->get()->result_array();
$this->db->set_dbprefix('tc_');