Fórum agrupar dados de tabela em uma linha #457508
04/10/2013
0
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
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
Curtir tópico
+ 0Posts
07/10/2013
Felippe Tadeu
Na tabela seria um registro pra cada contato ou vários registros pra um contato ?
Gostei + 0
07/10/2013
Michael Correia
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
07/10/2013
Felippe Tadeu
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
07/10/2013
Michael Correia
Gostei + 0
07/10/2013
Felippe Tadeu
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
07/10/2013
Michael Correia
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
08/10/2013
Felippe Tadeu
Gostei + 0
23/10/2013
José
Fiquem a vontade para abrir novos tópicos.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)