从4个表中获取信息并进行操作?

时间:2012-11-30 17:46:22

标签: php oracle

我在操纵信息方面遇到很多困难,以达到理想的效果。通过查询我加入以下结果:

   Area     |  ID_AT  |     AT    | TYPE
-----------------------------------------
Informatica |    1    |  Sistemaa |  E
Informatica |    3    | engraçado |  E
Informatica |    3    | engraçado |  I
Gestão      |    2    |   aaaaa   |  I

查询:

select a.Area, c.ID_AT, c.AT, dest.type 
from AREA_AT a
left join AT_C c on a.id_AREA_AT = c.id_AREA_AT
left join dest_atv d on d.id_AT = c.id_at and d.id_uo = c.id_uo
left join CLIE dest on d.id_CLIE = dest.id_CLIE
where id.uo = 1222 
order by a.id_AREA_AT, c.id_at, dest.type

但我想要的是在php中创建一个表,如下所示:

   Area     |  ID_AT  |     AT    | E | I
-------------------------------------------
Informatica |    1    |  Sistemaa | X |
Informatica |    3    | engraçado | X | X
Gestão      |    2    |   aaaaa   |   | X

简而言之,我打算在这里做的只是显示一个表ID,显示可能存在或不存在类型I或E,具体取决于来自查询的内容。

我是否必须修改查询以方便自己的工作?或者我将不得不在PHP中创建一个相当复杂的算法来执行此功能?安藤在这里转了几个小时,仍然找不到可以帮助我的解决方案吗?

1 个答案:

答案 0 :(得分:0)

听起来你需要做pivot query。像

这样的东西
select a.Area, 
       c.ID_AT, 
       c.AT, 
       max( case when dest.type = 'E' then 'X' else null end) E,
       max( case when dest.type = 'I' then 'X' else null end) I
  from AREA_AT a
       left join AT_C c on a.id_AREA_AT = c.id_AREA_AT
       left join dest_atv d on d.id_AT = c.id_at and d.id_uo = c.id_uo
       left join CLIE dest on d.id_CLIE = dest.id_CLIE
 where id.uo = 1222 
 group by a.area, c.id_at, c.at
 order by a.area, c.id_at

请注意,如果您未在此配方中选择(并按分组)该列,则您将无法ORDER BY a.id_area_at列。我将其更改为按a.area排序。