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

f5emj3cl  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(239)

我在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` 只做一个。我很感激这些信息,并为大家提供了很多帮助
我希望我已经解释清楚了
pjngdqdw

pjngdqdw1#

不能对每个查询中的不同字段进行联合,select子句中的字段在两个查询中必须相同。

相关问题