为什么用UNION显示这样的数据

时间:2018-07-17 18:27:29

标签: php mysql codeigniter

我正在codeigniter中执行UNION,我希望数据显示如下。

[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
                )
          )

我的模型中有以下查询,其中我对表Maintenance进行查询,但我也对表Activities进行了查询WHERE

$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();

通过此查询,我得到了这些值

[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
            )
 )

我应该不应该选择[nro] => meeting而不是[activity] => meeting select,或者我应该编辑GUI.h并只选择一个。我非常感谢您提供的信息,并为所有人致以深深的问候

我希望我对我的解释很好

1 个答案:

答案 0 :(得分:2)

您不能在每个查询中对不同的字段进行UNION,两个查询中的SELECT子句中的字段必须相同。