I have an SQL Query like this
return $this->db->query("SELECT * FROM `rincian_permintaan`
JOIN `permintaan` ON `permintaan`.`id_permintaan` = `rincian_permintaan`.`id_permintaan`
JOIN `users` ON `permintaan`.`id_peminta` = `users`.`user_id`
JOIN `cabang` ON `cabang`.`id_cabang` = `users`.`id_cabang`
JOIN `barang` ON `barang`.`id_barang` = `rincian_permintaan`.`id_barang`
JOIN `po` ON `rincian_permintaan`.`id_po` = `po`.`id_po` WHERE `po`.`id_cabang` = '201' AND `users`.`id_cabang` != 201 AND
NOT EXISTS(SELECT * FROM airwaybill WHERE airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan)
ORDER BY rincian_permintaan.created_at DESC")->result_array();
I want to change the format to:
$this->db->select('*');
$this->db->from('tableName');
$this->db->join('...');
$this->db->where('...');
And this is what i've tried:
$this->db->select('*');
$this->db->from('rincian_permintaan');
$this->db->join('permintaan', 'permintaan.id_permintaan = rincian_permintaan.id_permintaan');
$this->db->join('users', 'permintaan.id_peminta = users.user_id');
$this->db->join('cabang', 'cabang.id_cabang = users.id_cabang');
$this->db->join('barang', 'barang.id_barang = rincian_permintaan.id_barang');
$this->db->join('po', 'rincian_permintaan.id_po = po.id_po');
$this->db->where('po.id_cabang', '201');
$this->db->where('users.id_cabang != 201');
$this->db->select('*');
$this->db->from('airwaybill');
$this->db->where('NOT EXISTS airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan', '', FALSE);
return $this->db->get('rincian_permintaan')->result_array();
Error:
All you need is to generate sql string for subquery without executing it and use it in the outer query and produce the result.
$this->db->select('*');
$this->db->from('airwaybill');
$this->db->where('airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan');
$sub_query = $this->db->get_compiled_select();
$this->db->select('*');
$this->db->from('rincian_permintaan');
$this->db->join('permintaan', 'permintaan.id_permintaan = rincian_permintaan.id_permintaan');
$this->db->join('users', 'permintaan.id_peminta = users.user_id');
$this->db->join('cabang', 'cabang.id_cabang = users.id_cabang');
$this->db->join('barang', 'barang.id_barang = rincian_permintaan.id_barang');
$this->db->join('po', 'rincian_permintaan.id_po = po.id_po');
$this->db->where('po.id_cabang', '201');
$this->db->where('users.id_cabang != 201 ');
$this->db->where('NOT EXISTS('.$sub_query.')');
$query = $this->db->get();
$result = $query->result_array();
return $result;
$sql = "select * from tbl_user";
$result = $this->doSelect($sql);
foreach ($result as $key => $row) {
$sql = "select * from tbl_comment order by id desc";
$result = $this->doSelect($sql);
$result[$key]['idduc'] = $result;
}
return $result;
}
You are looping on a variable called $result, from the first query.
In the second query you destroy $result by reusing it in the second query processing. Also you then destroy that $result by assigning things to it as an array you want to return.
So use a different variable for the inner look like this
$sql = "select * from tbl_user";
$result = $this->doSelect($sql);
foreach ($result as $key => $row) {
$sql = "select * from tbl_comment order by id desc";
$result1 = $this->doSelect($sql);
$return[$key]['idduc'] = $result1;
}
return $return;
}
I'm trying almost everything to add a new key and its value to a result query.
$consulta = "SELECT p.id_empleado,count(p.id_empleado) as pendientes,e.nombre,e.apellidos FROM partidas_empleados p ";
$consulta.= "inner join empleados e on p.id_empleado=e.id_empleado ";
$consulta.= "WHERE abierta=TRUE group by id_empleado";
$sql = $con->prepare($consulta);
$ok = $sql->execute();
$query = $sql->fetchAll(PDO::FETCH_ASSOC);
for($i=0;$i<count($query);$i++){
//echo "hola";
$fila = $query[$i];
$consulta = "SELECT id_partida FROM partidas_empleados where id_empleado=? ";
$sql = $con->prepare($consulta);
$ok = $sql->execute(array($fila['id_empleado']));
$sub_query = $sql->fetchAll(PDO::FETCH_ASSOC);
//echo $sub_query;
//$fila[]= array("lista_partidas"=>$sub_query);
$fila['lista_partidas']= $sub_query;
}
$sub_query is just a list of asociative arrays
I'm trying to add $sub_query to $query with lista_partidas as a key.
As #kunruh and #jeroen said I was creating a copy of and modifying that copy.
$consulta = "SELECT p.id_empleado,count(p.id_empleado) as pendientes,e.nombre,e.apellidos FROM partidas_empleados p ";
$consulta.= "inner join empleados e on p.id_empleado=e.id_empleado ";
$consulta.= "WHERE abierta=TRUE group by id_empleado";
$sql = $con->prepare($consulta);
$ok = $sql->execute();
$query = $sql->fetchAll(PDO::FETCH_ASSOC);
for($i=0;$i<count($query);$i++){
$consulta = "SELECT id_partida FROM partidas_empleados where id_empleado=? ";
$sql = $con->prepare($consulta);
$ok = $sql->execute(array($fila['id_empleado']));
$sub_query = $sql->fetchAll(PDO::FETCH_ASSOC);
$query[$i]['lista_partidas']= $sub_query;
}
I want to do a sql query like
select * where x = 10 and (y=12 or h=15)
How can I achieve that in CI ActiveRecord format?
See Active record reference
$where="x = 10 and (y=12 or h=15)";
$this->db->select('*');
$this->db->from('mytable');
$this->db->where($where);
$query = $this->db->get();
Try like
$sql = "SELECT * FROM TABLE_NAME WHERE x = 10 AND (y = 12 OR h = 15)";
$result = $this->db->query($sql);
Or like
$this->db->select('*');
$this->db->from('TABLE_NAME');
$this->db->where('x',10);
$this->db->AND('y',12);
$this->db->or_where('h',15);
$query = $this->db->get();
Is it possible to do something like this in codeigniter:
publi function getcat($category_id)
{
$query = "(SELECT cat.id, cat.title ";
$query = $query . "FROM bf_categories cat ";
$query = $query . "WHERE cat.id=" . $category_id;
$this->db->limit(2, 4);
$records = $this->db->query($query);
return $records->result();
}
I've simplified the query just for demo purposes... but it's actually quite complex, which is why I've decided to use the query() method.
But the limit clause is not being included in the query... I've verified by enabling the codeigniter profiler in my controller and I can see that the query is run with out any limit clause.
Can you tell me how I can accomplish this using the query() method?
Edit 1
I've modified my model to look like this:
public function get_categories_and_products($limit=5, $offset=0, $category_id=null)
{
print "<BR>the function got the following offeset: $offset and limit: $limit";
$query = "(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight ";
$query = $query."FROM bf_categories cat ";
$query = $query."WHERE cat.parent_id=".$category_id;
$query = $query." AND cat.category_id <>".$category_id;
$query = $query.") UNION (";
$query = $query."SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight";
$query = $query." FROM bf_product p ";
$query = $query."Inner join bf_product_category cp ";
$query = $query."on p.product_id=cp.product_id ";
$query = $query."Where cp.category_id=".$category_id.") ?";
$records = $this->db->query($query,array($this->db->limit(2, 4)));
return $records->result();
}
I've hardcoded the limit values for now... but ultimately, i'll be using the values that get passed into the method. Unfortunately, this code still does not work.
According to the profiler, here's what's being executed:
(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight FROM bf_categories cat WHERE cat.parent_id=3 AND cat.category_id <>3) UNION (SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight FROM bf_product p Inner join bf_product_category cp on p.product_id=cp.product_id Where cp.category_id=3)
SELECT * FROM (`bf_categories`) WHERE `category_id` = '3' LIMIT 4, 2
So it's creating two separate select statements.
I tried this way, multiple join with start and limit. you remove join as per choice.
$this->db->select('*');
$this->db->from('tbl_requestservice as r');
$this->db->join('tbl_service as s','s.service_id=r.service_id');
$this->db->limit($limit, $start);
$query = $this->db->get();
print_r($query->result());
return $query->result();
This is how a query looks like in CI:
$this->db->select("id, title");
$this->db->where("id", $category_id);
$this->db->from("bf_categories");
$this->db->limit($limit);
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
echo $row->title;
}
}
If you include the full query, it would be easier to solve the problem or show you a better and more efficient approach.
In the end, I'm doing everything manually.
public function get_categories_and_products($limit=10, $offset=0, $category_id=null)
{
$query = "(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight ";
$query = $query."FROM bf_categories cat ";
$query = $query."WHERE cat.parent_id=".$category_id;
$query = $query." AND cat.category_id <>".$category_id;
$query = $query.") UNION (";
$query = $query."SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight";
$query = $query." FROM bf_product p ";
$query = $query."Inner join bf_product_category cp ";
$query = $query."on p.product_id=cp.product_id ";
$query = $query."Where cp.category_id=".$category_id.") limit ".$offset.','.$limit;
$catsandprods= $this->db->query($query);
return $catsandprods->result();
}
http://code-igniter.ru/user_guide/database/active_record.html
$this->db->start_cache();
$this->db->select('field1');
$this->db->stop_cache();
$this->db->get('tablename');
SELECT field1 FROM (tablename)
$this->db->select('field2');
$this->db->limit(10);
$this->db->get('tablename');
// SELECT field1, field2 FROM (tablename) limit 10
$this->db->flush_cache();
$this->db->select('field2');
$this->db->get('tablename');
// SELECT field2 FROM (tablename)
you can use
$query = "(SELECT cat.id, cat.title ";
$query = $query."FROM bf_categories cat ";
$query = $query."WHERE cat.id=".$category_id." ?";
$this->db->query($query,array($this->db->limit(2, 4)));
or
$this->db->select('id,title')->from('bf_categories ')->where('id', $id)->limit(2, 4);
link to doc