我正在使用codeigniter并使用jquery datatable进行列表。在codeigniter中我使用了数据库,我必须在我的sql查询中使用order by和union
mysql查询看起来像
SELECT date_format(cp_cuspaydate, '%d-%m-%Y %h:%i:%s %p') as transdate,cp_cusaccno as accno,'Purchase' as particulars,cp_cusamount as amount,
cp_qty as crqty,'-' as drqty, '' as blanceqty
FROM chf_customerpayments
WHERE cp_cusid = 22
UNION
SELECT date_format(credm_reqdate, '%d-%m-%Y %h:%i:%s %p') as transdate,credm_accno as accno,'Redeem' as particulars,'-' as amount,
'-' as crqty,credm_weight as drqty, '' as blanceqty
FROM chf_customerredeemrequest
WHERE credm_cusid = 22 AND credm_status !=2
ORDER BY STR_TO_DATE(transdate,'%d-%m-%Y %h:%i:%s %p') asc;
我需要生成数据表,为此我尝试了以下代码
$this->datatables
->select("date_format(cp_cuspaydate, '%d-%m-%Y %h:%i:%s %p') as transdate,cp_cusaccno as accno,'Purchase' as particulars,cp_cusamount as amount,
cp_qty as crqty,'-' as drqty, '' as blanceqty", false)
->from('chf_customerpayments')
->where('cp_cusid',$this->session->userdata('user_id'))
->union('chf_customerredeemrequest')
->select("date_format(credm_reqdate, '%d-%m-%Y %h:%i:%s %p') as transdate,credm_accno as accno,'Redeem' as particulars,'-' as amount,
'-' as crqty,credm_weight as drqty, '' as blanceqty", false)
->where('credm_cusid = '.$this->session->userdata('user_id').' credm_status !=2');
echo $this->datatables->generate();
但是数据库库中没有联合函数。如何为我的查询生成数据表。请提前帮助任何人,
答案 0 :(得分:1)
对于codeigniters活动记录,特别是在嵌套,联合等情况出现时,不可能进行稍微复杂的查询(例如此查询)。
使用$this->db->query("...");
来运行此查询"旧式的方式"。
请务必通过$this->db->escape(variable)
主要在这种情况下,编码器的活动记录不是设计为具有多个"选择"在一个查询中。这意味着即使你添加" union"关键字(作为评论建议)它将所有"选择"在一个声明中。
答案 1 :(得分:1)
我会单独执行两个查询$ query1& $ QUERY2。 然后合并结果集,如下所示:
$ union = array_merge($ query1,$ query2);