untested code
I couldn’t test it here, but here’s the idea:
- create a temporary table to store what matches each
jovem
and monitor
who live in the same city;
- in the query, add the total of "coincidences"
Try to do some tests, in case the logic isn’t right, we try to tidy up.
CREATE TEMPORARY TABLE IF NOT EXISTS tabelaTemporaria AS (
SELECT jovem.jid as 'jid',
mentor.mid as 'mid',
CASE jovem.jfaixaetaria WHEN mentor.mfaixaetaria THEN 1 else 0 end as 'faixaetaria',
CASE jovem.jescolaridade WHEN mentor.mescolaridade THEN 1 else 0 end as 'escolaridade',
CASE jovem.jhobby WHEN mentor.mhobby THEN 1 else 0 end as 'hobby',
CASE jovem.jcomida WHEN mentor.mcomida THEN 1 else 0 end as 'comida',
CASE jovem.jmusica WHEN mentor.mmusica THEN 1 else 0 end as 'musica',
CASE jovem.jesporte WHEN mentor.mesporte THEN 1 else 0 end as 'esporte',
CASE jovem.jtime WHEN mentor.mtime THEN 1 else 0 end as 'time',
CASE jovem.jcaracteristica WHEN mentor.mcaracteristica THEN 1 else 0 end as 'caracteristica',
CASE jovem.janimal WHEN mentor.manimal THEN 1 else 0 end as 'animal',
CASE jovem.jlivro WHEN mentor.mlivro THEN 1 else 0 end as 'livro',
CASE jovem.jsonho WHEN mentor.msonho THEN 1 else 0 end as 'sonho'
FROM jovem INNER JOIN mentor ON jovem.jcidade = mentor.mcidade AND jovem.jestado = mentor.mestado
)
SELECT jid, mid, (faixaetaria + escolaridade + hobby + comida + musica + esporte + time + caracteristica + animal + livro + sonho) as total_caracteristicas
FROM tabelaTemporaria
ORDER BY total_caracteristicas DESC
--LIMIT 2 --LIMIT é opcional para o total que precisar de retornos
Have you tried something? put your code to try to help. (https://answall.com/help/how-to-ask)
– rLinhares
I need to add the COUNT in the fields, the 2 that have more equal combinations will be selected.
– Carolina Perretti Cabral
blz, now what’s the problem with this query? error? returns too much? returns nothing?
– rLinhares
He makes no mistake... returns the "young man" who has the answers "equal" to the mentor’s... but I need him to make a "COUNT" from: the young 1 had 4 equal answers from some tutor - the young 2 had 5 equal answers to the tutor...
– Carolina Perretti Cabral
he returns the
jovem
the same because your query only brings if all characteristics are equal. but you want the one that has more characteristics in common, is that it? In the case of the 11 possible (not counting city and state) you want to know who has more (type jovem_1 has 6 equal with monitor_1, jovem_2 has 3 equal with monitor_2).. that’s it?– rLinhares
that’s right! I would only change to: jovem_1 has 6 equal with monitor_1, jovem_2 has 3 equal with monitor_1)
– Carolina Perretti Cabral
What are these features? the columns of each table(all) or have some that should not be compared as email and name?
– Caique Romero
Name, email, photo do not need to be compared
– Carolina Perretti Cabral