agrupar dados de tabela em uma linha

MySQL

04/10/2013

galera eu tenho a seguinte tabela:

CREATE TABLE tblcontato (
  cttCodigo int(11) NOT NULL AUTO_INCREMENT,
  cttTipo varchar(45) DEFAULT NULL,
  cttValor varchar(255) DEFAULT NULL,
  cttCpf varchar(11) DEFAULT NULL,
  PRIMARY KEY (cttCodigo)


onde o tipo pode ser :
telefone1
telefone2
celular
email

e gostaria de retornar em uma linha assim

cpf telefone1 telefone2 celular email
xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxx xxxxxx@xxxxxxx

alguem poderia me ajudar?

ja tentei usar o case no select mas ele retorna uma linha para cada contato.
Michael Correia

Michael Correia

Curtidas 0

Respostas

Felippe Tadeu

Felippe Tadeu

04/10/2013

Bom dia, desculpe, mas não entendi muito..
Na tabela seria um registro pra cada contato ou vários registros pra um contato ?
GOSTEI 0
Michael Correia

Michael Correia

04/10/2013

Alaplaia,

seria assim:
cttCodigo = autoincremento
cttTipo = telefone1 ou telefone2 ou celular ou email
cttValor = valor do contato
cttCpf = cpf do cliente

então um cliente teria vários contatos
eu tentei fazer assim:
 select     cttcpf ,   
(case `sistema_vendas`.`tblcontato`.`cttTipo`
            when 'TELEFONE1' then `sistema_vendas`.`tblcontato`.`cttValor`
        end) AS `telefone1`,
        (case `sistema_vendas`.`tblcontato`.`cttTipo`
            when 'TELEFONE2' then `sistema_vendas`.`tblcontato`.`cttValor`
        end) AS `telefone2`,
        (case `sistema_vendas`.`tblcontato`.`cttTipo`
            when 'EMAIL' then `sistema_vendas`.`tblcontato`.`cttValor`
        end) AS `email`,
        (case `sistema_vendas`.`tblcontato`.`cttTipo`
            when 'CELULAR' then `sistema_vendas`.`tblcontato`.`cttValor`
        end) AS `celular`
from `sistema_vendas`.`tblcontato` 
group by cttcpf,telefone1,telefone2,email,celular;


mas ele retorna uma linha para cada item na tabela

GOSTEI 0
Felippe Tadeu

Felippe Tadeu

04/10/2013

Tente isso
select cttcpf, (select cttValor from tblcontato where ccTipo = 'TELEFONE1' and cttCpf = "o cpf da pessoa"),
                       (select cttValor from tblcontato where ccTipo = 'TELEFONE2' and cttCpf = "o cpf da pessoa"), 
                       (select cttValor from tblcontato where ccTipo = 'EMAIL' and cttCPF = "o cpf da pessoa"),
                       (select cttValor from tblcontato where ccTipo = 'CELULAR' and cttCPF = "o cpf da pessoa")
from tblcontato
group by cttcpf
GOSTEI 0
Michael Correia

Michael Correia

04/10/2013

Bom isso funciona se for para um cliente especifico, agora se eu quiser criar uma view para usar para qualquer cliente nao funciona.
GOSTEI 0
Felippe Tadeu

Felippe Tadeu

04/10/2013

select a.cttcpf, (select cttValor from tblcontato where ccTipo = 'TELEFONE1' and cttCpf = a.cttCPF),
                       (select cttValor from tblcontato where ccTipo = 'TELEFONE2' and cttCpf = a.cttCPF), 
                       (select cttValor from tblcontato where ccTipo = 'EMAIL' and cttCPF = a.cttCPF),
                       (select cttValor from tblcontato where ccTipo = 'CELULAR' and cttCPF = a.cttCPF)
from tblcontato a
where a.cttCPF in (select cttCPF from tblcontatos)
group by a.cttcpf

Isso não daria certo ?
GOSTEI 0
Michael Correia

Michael Correia

04/10/2013

select a.cttcpf, (select cttValor from tblcontato where ccTipo = 'TELEFONE1' and cttCpf = a.cttCPF),
                       (select cttValor from tblcontato where ccTipo = 'TELEFONE2' and cttCpf = a.cttCPF), 
                       (select cttValor from tblcontato where ccTipo = 'EMAIL' and cttCPF = a.cttCPF),
                       (select cttValor from tblcontato where ccTipo = 'CELULAR' and cttCPF = a.cttCPF)
from tblcontato a
where a.cttCPF in (select cttCPF from tblcontatos)
group by a.cttcpf

Isso não daria certo ?


Alaplaia,

deu certo sim ... muito obrigado !
GOSTEI 0
Felippe Tadeu

Felippe Tadeu

04/10/2013

Por nada cara, tamos aí!!
GOSTEI 0
José

José

04/10/2013

Já que a duvida inicial foi sanada, estou marcando o tópico como concluído.
Fiquem a vontade para abrir novos tópicos.
GOSTEI 0
POSTAR