3

我有以下表格:

mysql> select * from `empresas`;
+----+-------+-----------+-------+------------+----------------+
| id | tipo  | logotipo  | nome  | grupo      | cnpj           |
+----+-------+-----------+-------+------------+----------------+
|  8 | Lazer | troll.jpg | Teste | Pespi Cola | 99999313412312 |
+----+-------+-----------+-------+------------+----------------+

mysql> select * from `empresas_contatos`;
+----+---------+------------+--------------+------------+
| id | empresa | rotulo     | email        | telefone   |
+----+---------+------------+--------------+------------+
|  1 |       8 | Principal  | x@xxx.co.co  | 5112121212 |
|  2 |       8 | Financeiro | fin@y.net    | 5012121212 |
+----+---------+------------+--------------+------------+

我想同时使用左连接,如下所示:

mysql> select `e`.`nome`, `e`.`grupo`, `c`.* from `empresas` `e` 
    -> left join `empresas_contatos` `c` on
    -> `c` . `empresa` = `e` . `id`;
+-------+------------+------+---------+------------+-------------+------------+
| nome  | grupo      | id   | empresa | rotulo     | email       | telefone   |
+-------+------------+------+---------+------------+-------------+------------+
| Teste | Pespi Cola |    1 |       8 | Principal  | x@xxx.co.co | 5112121212 |
| Teste | Pespi Cola |    2 |       8 | Financeiro | fin@y.net   | 5012121212 |
+-------+------------+------+---------+------------+-------------+------------+

问题在于,以这种方式,查询会重复empresas字段,例如grupo, nome(实际表比示例大!)。

我想知道如何在一行中接收所有数据。结果应该是这样的:

+-------+------------+------+---------+------------+-------------+------------+-------------+------------+
| nome  | grupo      | id   | empresa | rotulo     | email1      | telefone1  | email2      | telefone2  |
+-------+------------+------+---------+------------+-------------+------------+-------------+------------+
| Teste | Pespi Cola |    1 |       8 | Principal  | x@xxx.co.co | 5112121212 | fin@y.net   | 5012121212 |
+-------+------------+------+---------+------------+-------------+------------+-------------+------------+

如果有第三empresas_contatos行,查询将返回email3, telefone3...

我可以使用 GROUP_CONCAT(),但我很想为此找到解决方案。

提前致谢!

4

2 回答 2

2

您实际上是在尝试做一个PIVOT. 这里的问题是我们需要行标识符来PIVOT反对。这就是我介绍按您的 empresa 列分组的行号的地方。

所以这样的事情应该可以工作,但你需要知道潜在列的最大数量。但是,这应该可以帮助您入门:

select g.empresa,
  MAX(CASE WHEN rownum = 1 THEN g.email END) Email1,
  MAX(CASE WHEN rownum = 2 THEN g.email END) Email2,
  MAX(CASE WHEN rownum = 1 THEN g.telefone END) Phone1,
  MAX(CASE WHEN rownum = 2 THEN g.telefone END) Phone2
from (
    select id,empresa,email,telefone,
        @running:=if(@previous=empresa,@running,0) + 1 as rownum,
        @previous:=empresa
    from empresas_contatos t
      JOIN (SELECT @running:= 0) r
      JOIN (SELECT @previous:= 0) s
) g 
GROUP BY g.empresa

这是SQL Fiddle

顺便说一句-看起来您的 rotulo 列也需要与重复项相同的逻辑。

这是一个更实用的工作示例来满足您的需求——我删除了 empresas_contatos.id,但如果您需要,只需添加另一个 MAX CASE:

select e.nome, e.grupo, g.empresa,
  MAX(CASE WHEN rownum = 1 THEN g.rotulo END) Rotulo1,
  MAX(CASE WHEN rownum = 2 THEN g.rotulo END) Rotulo2,
  MAX(CASE WHEN rownum = 1 THEN g.email END) Email1,
  MAX(CASE WHEN rownum = 2 THEN g.email END) Email2,
  MAX(CASE WHEN rownum = 1 THEN g.telefone END) Phone1,
  MAX(CASE WHEN rownum = 2 THEN g.telefone END) Phone2
from (
    select id,empresa,email,telefone,rotulo,
        @running:=if(@previous=empresa,@running,0) + 1 as rownum,
        @previous:=empresa
    from empresas_contatos t
      JOIN (SELECT @running:= 0) r
      JOIN (SELECT @previous:= 0) s
) g JOIN empresas e on g.empresa = e.id
GROUP BY e.nome, e.grupo, g.empresa

还有更多小提琴

于 2013-02-12T01:19:29.727 回答
1

这样做的原因是什么?运行此查询后如何使用数据?您是否将其导入电子表格或以其他方式处理?

对数据进行后处理以达到您想要的效果可能会更容易。

于 2013-02-12T01:51:12.927 回答