Fórum Query para coletar dados de várias colunas da mesma linha, por Primary Key. #608662
17/03/2020
0
O que preciso é algo bem simples, acredito!
Tenho uma tabela (resposta_usuario) no BD que é para armazenar as respostas de um questionário. Nela há os dados de quem o preenche (nome, idade, cpf, etc...) e os dados das respostas (que vai de "resp1" até "resp80", sendo 80 perguntas). As opções de respostas são 4 (A, T, G e F). Ao final do preenchimento do questionário, preciso consultar na tabela (resposta_usuario) as respostas de 1 a 80 as letras que o usuário selecionou e quantas vezes essas letras se repetem em suas respostas.
Por exemplo, supondo que eu (Myller) responda ao questionário, receba o ID (primary key) 1 na tabela. Tenho como resposta "resp1" = A, "resp2" = A, "resp3" = G, "resp4" = F e por aí vai até chegar a "resp80".
O que preciso é:
De uma Query que me dê o número de vezes que a letra "A" aparece nas minhas (usuário Myller, de ID 1 da tabela resposta_usuario) respostas (que no caso, é 2 vezes), e a mesma coisa para as letras "T", "G" e "F".
Espero ter sido claro e agradeço a ajuda de todos!

Myller Meireles
Curtir tópico
+ 0Post mais votado
18/03/2020
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | SELECT TAB.tr_id, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'A' , '' )) LETRAA, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'T' , '' )) LETRAT, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'G' , '' )) LETRAG, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'F' , '' )) LETRAF FROM ( SELECT tr_id, concat( tr_1 ,tr_2 ,tr_3 ,tr_4 ,tr_5 ,tr_6 ,tr_7 ,tr_8 ,tr_9 ,tr_10, tr_11,tr_12,tr_13,tr_14,tr_15,tr_16,tr_17,tr_18,tr_19,tr_20, tr_21,tr_22,tr_23,tr_24,tr_25,tr_26,tr_27,tr_28,tr_29,tr_30, tr_31,tr_32,tr_33,tr_34,tr_35,tr_36,tr_37,tr_38,tr_39,tr_40, tr_41,tr_42,tr_43,tr_44,tr_45,tr_46,tr_47,tr_48,tr_49,tr_50, tr_51,tr_52,tr_53,tr_54,tr_55,tr_56,tr_57,tr_58,tr_59,tr_60, tr_61,tr_52,tr_63,tr_64,tr_65,tr_66,tr_67,tr_68,tr_69,tr_70, tr_71,tr_52,tr_73,tr_74,tr_75,tr_76,tr_77,tr_78,tr_79,tr_80, tr_81 ) RESPOSTAS FROM tetra_respostas WHERE tr_id = 10 ) TAB |
Emerson Nascimento

Gostei + 1
Mais Posts
17/03/2020
Emerson Nascimento
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SELECT TAB.ID, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'A' , '' )) LETRAA, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'T' , '' )) LETRAT, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'G' , '' )) LETRAG, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'F' , '' )) LETRAF, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, ' ' , '' )) SEMPRESPOSTA FROM ( SELECT ID, resp1 +resp2 +resp3 +resp4 +resp5 +resp6 +resp7 +resp8 +resp9 +resp10+ resp11+resp12+resp13+resp14+resp15+resp16+resp17+resp18+resp19+resp20+ resp21+resp22+resp23+resp24+resp25+resp26+resp27+resp28+resp29+resp30+ resp31+resp32+resp33+resp34+resp35+resp36+resp37+resp38+resp39+resp40+ resp41+resp42+resp43+resp44+resp45+resp46+resp47+resp48+resp49+resp50+ resp51+resp52+resp53+resp54+resp55+resp56+resp57+resp58+resp59+resp60+ resp61+resp52+resp63+resp64+resp65+resp66+resp67+resp68+resp69+resp70+ resp71+resp52+resp73+resp74+resp75+resp76+resp77+resp78+resp79+resp80 RESPOSTAS FROM resposta_usuario ) TAB |
se os campos de resposta admitirem conteúdo NULL, utilize a função COALESCE() para pode obter o resultado desejado
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | SELECT TAB.ID, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'A' , '' )) LETRAA, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'T' , '' )) LETRAT, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'G' , '' )) LETRAG, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'F' , '' )) LETRAF, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, ' ' , '' )) SEMPRESPOSTA FROM ( SELECT ID, COALESCE (resp1 , ' ' )+ COALESCE (resp2 , ' ' )+ COALESCE (resp3 , ' ' )+ COALESCE (resp4 , ' ' )+ COALESCE (resp5 , ' ' )+ COALESCE (resp6 , ' ' )+ COALESCE (resp7 , ' ' )+ COALESCE (resp8 , ' ' )+ COALESCE (resp9 , ' ' )+ COALESCE (resp10, ' ' )+ COALESCE (resp11, ' ' )+ COALESCE (resp12, ' ' )+ COALESCE (resp13, ' ' )+ COALESCE (resp14, ' ' )+ COALESCE (resp15, ' ' )+ COALESCE (resp16, ' ' )+ COALESCE (resp17, ' ' )+ COALESCE (resp18, ' ' )+ COALESCE (resp19, ' ' )+ COALESCE (resp20, ' ' )+ COALESCE (resp21, ' ' )+ COALESCE (resp22, ' ' )+ COALESCE (resp23, ' ' )+ COALESCE (resp24, ' ' )+ COALESCE (resp25, ' ' )+ COALESCE (resp26, ' ' )+ COALESCE (resp27, ' ' )+ COALESCE (resp28, ' ' )+ COALESCE (resp29, ' ' )+ COALESCE (resp30, ' ' )+ COALESCE (resp31, ' ' )+ COALESCE (resp32, ' ' )+ COALESCE (resp33, ' ' )+ COALESCE (resp34, ' ' )+ COALESCE (resp35, ' ' )+ COALESCE (resp36, ' ' )+ COALESCE (resp37, ' ' )+ COALESCE (resp38, ' ' )+ COALESCE (resp39, ' ' )+ COALESCE (resp40, ' ' )+ COALESCE (resp41, ' ' )+ COALESCE (resp42, ' ' )+ COALESCE (resp43, ' ' )+ COALESCE (resp44, ' ' )+ COALESCE (resp45, ' ' )+ COALESCE (resp46, ' ' )+ COALESCE (resp47, ' ' )+ COALESCE (resp48, ' ' )+ COALESCE (resp49, ' ' )+ COALESCE (resp50, ' ' )+ COALESCE (resp51, ' ' )+ COALESCE (resp52, ' ' )+ COALESCE (resp53, ' ' )+ COALESCE (resp54, ' ' )+ COALESCE (resp55, ' ' )+ COALESCE (resp56, ' ' )+ COALESCE (resp57, ' ' )+ COALESCE (resp58, ' ' )+ COALESCE (resp59, ' ' )+ COALESCE (resp60, ' ' )+ COALESCE (resp61, ' ' )+ COALESCE (resp52, ' ' )+ COALESCE (resp63, ' ' )+ COALESCE (resp64, ' ' )+ COALESCE (resp65, ' ' )+ COALESCE (resp66, ' ' )+ COALESCE (resp67, ' ' )+ COALESCE (resp68, ' ' )+ COALESCE (resp69, ' ' )+ COALESCE (resp70, ' ' )+ COALESCE (resp71, ' ' )+ COALESCE (resp52, ' ' )+ COALESCE (resp73, ' ' )+ COALESCE (resp74, ' ' )+ COALESCE (resp75, ' ' )+ COALESCE (resp76, ' ' )+ COALESCE (resp77, ' ' )+ COALESCE (resp78, ' ' )+ COALESCE (resp79, ' ' )+ COALESCE (resp80, ' ' ) RESPOSTAS FROM resposta_usuario ) TAB |
Gostei + 0
17/03/2020
Myller Meireles
Ratificando: Os campos de respostas não admitem valores NULL. Eles são VARCHAR(1), Not NULL.
Porém, ao executar a query, o erro foi exibido: "Coluna 'ID' desconhecida em 'field list'"
Eu, não tenho ideia como resolver isso!
Gostei + 0
17/03/2020
Emerson Nascimento
Gostei + 0
17/03/2020
Myller Meireles
Você disse que existe o campo ID na tabela. Pelo jeito não existe.
Mas existe sim, até disse que (hipoteticamente) recebi o ID 1, como primary key. Lembra?
Na minha tabela esse campo é o "tr_id", no qual substituí por:
SELECT TAB.tr_id, (...)
Não é isso? Se for, o erro persiste.
Gostei + 0
17/03/2020
Emerson Nascimento
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SELECT TAB.tr_id, -- troquei aqui LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'A' , '' )) LETRAA, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'T' , '' )) LETRAT, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'G' , '' )) LETRAG, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, 'F' , '' )) LETRAF, LEN(TAB.RESPOSTAS)-LEN( REPLACE (TAB.RESPOSTAS, ' ' , '' )) SEMPRESPOSTA FROM ( SELECT tr_id, -- troquei aqui resp1 +resp2 +resp3 +resp4 +resp5 +resp6 +resp7 +resp8 +resp9 +resp10+ resp11+resp12+resp13+resp14+resp15+resp16+resp17+resp18+resp19+resp20+ resp21+resp22+resp23+resp24+resp25+resp26+resp27+resp28+resp29+resp30+ resp31+resp32+resp33+resp34+resp35+resp36+resp37+resp38+resp39+resp40+ resp41+resp42+resp43+resp44+resp45+resp46+resp47+resp48+resp49+resp50+ resp51+resp52+resp53+resp54+resp55+resp56+resp57+resp58+resp59+resp60+ resp61+resp52+resp63+resp64+resp65+resp66+resp67+resp68+resp69+resp70+ resp71+resp52+resp73+resp74+resp75+resp76+resp77+resp78+resp79+resp80 RESPOSTAS FROM resposta_usuario ) TAB |
Gostei + 0
18/03/2020
Myller Meireles
Fiz algumas pequenas modificações, são elas:
- Nas linhas 3 a 7, a função LEN dava "erro de função não existente", então troquei por LENGHT e rodou;
- Na linha 7, inseri o valor 0 (zero), pois não há resposta em branco/null;
- Na linha 20, inseri o "where tr_id = 10", que é um valor dinâmico que vem da variável URL.
Segue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SELECT TAB.tr_id, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'A' , '' )) LETRAA, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'T' , '' )) LETRAT, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'G' , '' )) LETRAG, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'F' , '' )) LETRAF, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, '0' , '' )) SEMPRESPOSTA FROM ( SELECT tr_id, tr_1 +tr_2 +tr_3 +tr_4 +tr_5 +tr_6 +tr_7 +tr_8 +tr_9 +tr_10+ tr_11+tr_12+tr_13+tr_14+tr_15+tr_16+tr_17+tr_18+tr_19+tr_20+ tr_21+tr_22+tr_23+tr_24+tr_25+tr_26+tr_27+tr_28+tr_29+tr_30+ tr_31+tr_32+tr_33+tr_34+tr_35+tr_36+tr_37+tr_38+tr_39+tr_40+ tr_41+tr_42+tr_43+tr_44+tr_45+tr_46+tr_47+tr_48+tr_49+tr_50+ tr_51+tr_52+tr_53+tr_54+tr_55+tr_56+tr_57+tr_58+tr_59+tr_60+ tr_61+tr_52+tr_63+tr_64+tr_65+tr_66+tr_67+tr_68+tr_69+tr_70+ tr_71+tr_52+tr_73+tr_74+tr_75+tr_76+tr_77+tr_78+tr_79+tr_80+tr_81 RESPOSTAS FROM tetra_respostas where tr_id = 10 ) TAB |
Apesar da consulta rodar, o resultado não é o esperado. As colunas LETRAA, LETRAT, LETRAG, LETRAF, todas estão com zero e a coluna SEMRESPOSTA, com 1, quando o resultado esperado seria exibir a quantidade de letras A, T, G, F e 0 (zero) repetidas no questionário de ID 10, por exemplo. Entendeu?
Além disso, mais abaixo aparece um alerta: "Warning: #1292 Truncado errado DOUBLE valor: 'A'"
Segue link da imagem dos resultados: https://drive.google.com/open?id=1eumHLCVeW4LTW1_RnjNICZMU3DT7S4iV
Gostei + 0
18/03/2020
Myller Meireles
Cara, agora sim deu 100% certo a parte do SQL!!!
O que estou precisando (para finalizar, em definitivo), é trazer os resultados das colunas "LETRAA", "LETRAT", "LETRAG" e "LETRAF" que estão no SQL para a tela do browser (to usando o PHP), mas não sei como "desenrolar" o que preciso para isso acontecer, no código da query.
Para vc entender melhor, segue abaixo o link da imagem com os resultados esperados.
https://drive.google.com/file/d/1b38ej67PYj1VVV7nPppByrWRCUhFr5nD/view?usp=sharing
Obrigado!
Gostei + 0
19/03/2020
Myller Meireles
Faltou complementar minha resposta de ontem... o código PHP que estou usando é esse para trazer os dados da consulta e mesmo assim não exibe nada.
1 2 3 4 5 6 | <?php echo $row_resposta[ 'tr_id' ]; ?> <?php echo $row_resposta[ 'RESPOSTAS' ]; ?> <?php echo $row_resposta[ 'LETRAA' ]; ?> <?php echo $row_resposta[ 'LETRAT' ]; ?> <?php echo $row_resposta[ 'LETRAG' ]; ?> <?php echo $row_resposta[ 'LETRAF' ]; ?> |
O que poderia ser?
Grato!
OBS: Não sei porquê o código PHP saiu com aspas duplas.
Gostei + 0
21/03/2020
Myller Meireles
Eles estão nas linhas 17 e 17, em: "tr_52" e "tr_52", onde o correto é: "tr_62" e "tr_t2", respectivamente... por isso os resultados não estavam sendo exibidos como esperado.
Gostei + 0
21/03/2020
Myller Meireles
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | SELECT TAB.tr_id, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'A' , '' )) LETRAA, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'T' , '' )) LETRAT, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'G' , '' )) LETRAG, LENGTH(TAB.RESPOSTAS)-LENGTH( REPLACE (TAB.RESPOSTAS, 'F' , '' )) LETRAF FROM ( SELECT tr_id, concat( tr_1 ,tr_2 ,tr_3 ,tr_4 ,tr_5 ,tr_6 ,tr_7 ,tr_8 ,tr_9 ,tr_10, tr_11,tr_12,tr_13,tr_14,tr_15,tr_16,tr_17,tr_18,tr_19,tr_20, tr_21,tr_22,tr_23,tr_24,tr_25,tr_26,tr_27,tr_28,tr_29,tr_30, tr_31,tr_32,tr_33,tr_34,tr_35,tr_36,tr_37,tr_38,tr_39,tr_40, tr_41,tr_42,tr_43,tr_44,tr_45,tr_46,tr_47,tr_48,tr_49,tr_50, tr_51,tr_52,tr_53,tr_54,tr_55,tr_56,tr_57,tr_58,tr_59,tr_60, tr_61,tr_62,tr_63,tr_64,tr_65,tr_66,tr_67,tr_68,tr_69,tr_70, tr_71,tr_72,tr_73,tr_74,tr_75,tr_76,tr_77,tr_78,tr_79,tr_80, tr_81 ) RESPOSTAS FROM tetra_respostas WHERE tr_id = 10 ) TAB |
Finalmente achei o erro!!!
Eles estão nas linhas 17 e 18 da query, em: "tr_52" e "tr_52", onde o correto é: "tr_62" e "tr_72", respectivamente... por isso os resultados não estavam sendo exibidos como esperado.
Sempre suspeitei de algum erro no SQL... ufa!!! Segue o jogo... valeu Emerson!
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)