MySQL使用单行左连接

时间:2013-02-12 01:02:50

标签: mysql sql

我有以下表格:

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字段,例如gruponome(真实表格大于示例!)。

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

+-------+------------+------+---------+------------+-------------+------------+-------------+------------+
| 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,则查询将返回email3telefone3 ...

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

提前致谢!

2 个答案:

答案 0 :(得分: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

更多Fiddle

答案 1 :(得分:1)

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

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