Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在CodeIgniter查询生成器中使用子查询编写查询?_Php_Codeigniter_Query Builder - Fatal编程技术网

Php 在CodeIgniter查询生成器中使用子查询编写查询?

Php 在CodeIgniter查询生成器中使用子查询编写查询?,php,codeigniter,query-builder,Php,Codeigniter,Query Builder,我有以下查询,它与查询生成器有问题 选择SumTEMP.total作为总金额 从中选择Ifnullt3.amount,t1.amount作为总计 从表1中取t1 左键将表2连接为t2 关于t2.class=t1.class 左连接表3作为t3 关于t3.student=t2.student t3.type=t1.type作为温度 使用查询生成器有什么方法可以做到这一点吗?我目前正在使用此方法。请检查此方法,希望能对您有所帮助 $this->db->select('IFNULL(t3.

我有以下查询,它与查询生成器有问题

选择SumTEMP.total作为总金额 从中选择Ifnullt3.amount,t1.amount作为总计 从表1中取t1 左键将表2连接为t2 关于t2.class=t1.class 左连接表3作为t3 关于t3.student=t2.student t3.type=t1.type作为温度
使用查询生成器有什么方法可以做到这一点吗?我目前正在使用此方法。

请检查此方法,希望能对您有所帮助

$this->db->select('IFNULL(t3.amount, t1.amount) as total');
$this->db->from('table1 as t1');
$this->db->join('table2 as t2', 't2.class = t1.class', 'LEFT');
$this->db->join('table3 as t3', 't3.student = t2.student AND t3.type = t1.type', 'LEFT');
// $this->db->get();
$lastquery = $this->db->last_query();

$this->db->select('SUM(TEMP.total) as total_amount');
$this->db->from('('.$lastquery.') as TEMP');
$result = $this->db->get()->result_array();

请核对一下,希望能对您有所帮助

$this->db->select('IFNULL(t3.amount, t1.amount) as total');
$this->db->from('table1 as t1');
$this->db->join('table2 as t2', 't2.class = t1.class', 'LEFT');
$this->db->join('table3 as t3', 't3.student = t2.student AND t3.type = t1.type', 'LEFT');
// $this->db->get();
$lastquery = $this->db->last_query();

$this->db->select('SUM(TEMP.total) as total_amount');
$this->db->from('('.$lastquery.') as TEMP');
$result = $this->db->get()->result_array();

如果您的mysql查询没有问题,请使用此方法运行这样复杂的查询

$sql = "Your Query";
$qr = $this->db->query($sql);
return $qr->row();

如果仍然没有得到输出,请首先在from子句中检查子查询。

如果mysql查询正常,请使用此方法运行类似这样的复杂查询

$sql = "Your Query";
$qr = $this->db->query($sql);
return $qr->row();
如果仍然没有得到输出,请首先在from子句中检查子查询。

不要使用get,后面跟着最后一个查询,因为get实际上会在数据库上运行查询

相反,使用get_compiled_select返回查询,而不运行查询

$this->db->select('IFNULL(t3.amount, t1.amount) as total');
$this->db->from('table1 as t1');
$this->db->join('table2 as t2', 't2.class = t1.class', 'LEFT');
$this->db->join('table3 as t3', 't3.student = t2.student AND t3.type = t1.type', 'LEFT');
$subquery = $this->db->get_compiled_select();


$this->db->select('SUM(TEMP.total) as total_amount');
$this->db->from('('.$subquery.') as TEMP');
$result = $this->db->get()->result_array();
默认情况下,get_compiled_select将重置查询生成器

.

不要使用get后跟上一个查询,因为get实际上会在数据库上运行查询

相反,使用get_compiled_select返回查询,而不运行查询

$this->db->select('IFNULL(t3.amount, t1.amount) as total');
$this->db->from('table1 as t1');
$this->db->join('table2 as t2', 't2.class = t1.class', 'LEFT');
$this->db->join('table3 as t3', 't3.student = t2.student AND t3.type = t1.type', 'LEFT');
$subquery = $this->db->get_compiled_select();


$this->db->select('SUM(TEMP.total) as total_amount');
$this->db->from('('.$subquery.') as TEMP');
$result = $this->db->get()->result_array();
默认情况下,get_compiled_select将重置查询生成器


这就是我的想法。现在我将使用它。@romaltandel这个答案是错误的,get方法将实际运行查询。看我的答案。这就是我的想法。现在我将使用它。@romaltandel这个答案是错误的,get方法将实际运行查询。看看我的答案。