Query and Subquery in codeigniter

Asked

Viewed 363 times

0

Salve Galera , I’m breaking my balls to assemble this query in the IC model

"SELECT *, geral_cnpj as cnpj, geral_mesref as mesref, (SELECT CEILING(( (SELECT `geral_valor_total` as valormes from tbl_geral where `geral_cnpj`=cnpj and `geral_mesref`=mesref) - (SELECT `geral_valor_total`from tbl_geral as valormesanterior where `geral_cnpj`=cnpj and `geral_mesref`=mesref-1) ) / (SELECT `geral_valor_total`as valormes from tbl_geral where `geral_cnpj`=cnpj and `geral_mesref`=mesref)*100 ) ) as valor_percentual FROM tbl_geral";

To query is correct as I have already checked in Mysql and brings the results. I tried to use it this way (which I believe is not correct) and gives me the error

Error Number: HY000/1096
No tables used
SELECT *

follows the method below the model

public function listar_gerais_percentual(){

   $q="SELECT *, geral_cnpj as cnpj, geral_mesref as mesref, (SELECT CEILING(( (SELECT `geral_valor_total` as valormes from tbl_geral where `geral_cnpj`=cnpj and `geral_mesref`=mesref) - (SELECT `geral_valor_total`from tbl_geral as valormesanterior where `geral_cnpj`=cnpj and `geral_mesref`=mesref-1) ) / (SELECT `geral_valor_total`as valormes from tbl_geral where `geral_cnpj`=cnpj and `geral_mesref`=mesref)*100 ) ) as valor_percentual FROM tbl_geral";



  $result = $this->db->query($q); 

  return $result;

}

2 answers

0


Hello! $this->db->query() is not part of CI’s Active Record, but $this->db->get() yes, and this is an error that appears when a query has been improperly mounted via query Builder. For example, called the method get() no table name, or without having specified via from().

The error does not seem to be in this function that you showed. Check that in your control there are no other calls to other methods of the model. It is likely that in some other function of the model there is a get() unnecessary, or there is such a badly configured query.

0

Thanks for the help, but I solved it that way:

$query = $this->db->query($q);

Return $query->result_array();

//// IN THE CONTROLLER STAYED

$this->load->model('Data_model', 'data');

    $dados['gerais']=$this->dados->listar_gerais_percentual();

NOTE: I will try to use get() as a study to see if they are right (I believe that I need to call

Browser other questions tagged

You are not signed in. Login or sign up in order to post.