Reputation: 228
I'm doing a UNION in codeigniter and I want the data show like this.
[array] => Array
(
[0] => stdClass Object
(
//Table 1
[id_tecnico] => 1698
[date] => 2018-02-12
[nro] => M49320
[start] => 15:15
[end] => 17:45
[comment] => ok.
[total] => 2.5
)
[1] => stdClass Object
(
//Table 2
[id_tecnico] => 1698
[nro] => M49317
[date] => 2018-02-12
[activity] => meeting
[comment] => meeeting about nothing
[TotalHrs] => 0.67
)
)
I have the following query in my model, where I make the query to the table Maintenance
, but I also do a query to the table Activities
with the sameWHERE
$this->db->select('p.name,s.date,s.nro,d.start,d.end,d.comment,d.total,m.machine');
$this->db->from('Tecnico_Seguimiento as t');
$this->db->join('personal as p','p.Codigo = t.id_tecnico');
$this->db->join('MAN_SeguimientoDetalle as d','d.id_detalle = t.id_detalle');
$this->db->join('MAN_Seguimiento as s','s.idMan_Tecnico = d.id_man_tecnico');
$this->db->join('MAN_Solicitud as m','m.NroSolicitud = s.NroSolicitud');
$this->db->where('t.id_tecnico',$id);
$this->db->where('s.date >=',$minvalue);
$this->db->where('s.date <=',$maxvalue);
$query1 = $this->db->get_compiled_select();
$this->db->select('act.nro as nroAct, act.date as fechaAct,act.activity,act.comment as commnetAct, act.TotalHrs, act.orden, act.date as dateAct, act.id_tecnico ');
$this->db->from('MAN_Actividades as act');
$this->db->where('act.date >=',$minvalue);
$this->db->where('act.date <=',$maxvalue);
$this->db->where('act.id_tecnico <=',$id);
$query2 = $this->db->get_compiled_select();
return $this->db->query($query1." UNION ".$query2)->result();
With this query I get these values
[array] => Array
(
[0] => stdClass Object
(
[name] => HUGO
[date] => 2018-02-20
[nro] => M49301
[start] => 17:45
[end] => 19:30
[commet] => ok.
[total] => 1.75
[machine] => Torno
)
[1] => stdClass Object
(
// Here should be the fields of the table activities
[name] => M49321
[date] => 2018-02-12
[nro] => meeting
[start] => meeting about nothing
[end] => 1
[comment] => 49321
[total] => 17:43:00
[machine] => 1698
)
)
Instead of [nro] => meeting
it should be[activity] => meeting
I dunno why just some the first select or I should edit the select
and do just one. I appreciate the information and help a lot greeting for all
I hope I have explained me well
Upvotes: 0
Views: 35
Reputation: 131
You can't do UNION with different fields in each query, the fields in the SELECT clause must be the same on both queries.
Upvotes: 2