2

为了达到预期的结果,我在处理信息方面遇到了很多困难。通过查询,我加入了以下结果:

   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 ativ,显示可能存在或不存在类型 I 或 E,具体取决于查询的内容。

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

4

1 回答 1

0

听起来您需要进行数据透视查询。就像是

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

于 2012-11-30T17:56:05.800 回答