Query 2 tables in a sql database and display repeated values

Asked

Viewed 168 times

0

I have 2 tables in my database, a call pessoas and another call resultado, I am filling the table result with the draws made by mega sena, the table result has the fields:

data, dez1, dez2, dez3, dez4, dez5, dez6

Already the table people have the fields:

nome, dez1, dez1, dez3, dez4, dez5, dez6

I would like to make a select where show me exactly which balls people hit, in case show the tens that are equal in the two tables, they have no foreign key.

1 answer

1

One way to do maybe not the best but nevertheless possible would be to do the following:

SELECT `pessoas`.`name`, `pessoas`.`dez1` FROM resultado
INNER JOIN pessoas ON `resultado`.`dez1`=`pessoas`.`dez1`

This returns all table records pessoas with values equal to dez1 table resultado. To obtain the remaining results it is enough to change the value 1 by 2, 3, 4, 5 and 6.

Real example in SQL Fiddle.

EDIT1

I managed to improve the query, example:

SELECT pessoas.name, pessoas.dez1 FROM pessoas
WHERE dez1 IN (SELECT dez1 FROM resultado WHERE data=140317)
OR dez1 IN (SELECT dez2 FROM resultado WHERE data=140317)
OR dez1 IN (SELECT dez3 FROM resultado WHERE data=140317)
OR dez1 IN (SELECT dez4 FROM resultado WHERE data=140317)
OR dez1 IN (SELECT dez5 FROM resultado WHERE data=140317)
OR dez1 IN (SELECT dez6 FROM resultado WHERE data=140317); 

In this query we select the first value of all people, and then we compare if it is equal to any of the 6 of the result. It also includes the condition to know how to look for the right day.

Real example in SQL Fiddle.

  • in case I want to compare the dez1 of people to the 6 tens of the result, I have to repeat those 6 steps in the 6 tens ne

  • @Renancarvalho I did an update there answer, with your doubt, I hope to have been able to help

  • thank you very much my friend, unfortunately does not solve my case, but I thank from now on your help, I think this missing me knowledge, I go from the one studied better in another way to achieve this, I have tried in java and php but nothing, I want to make 2 tables where one had the results of the mega and the other the bets of the people, in every result of the mega sena the program would check in the bets of the people and mark how many balls each person hit and missed, if you know something I can study to get this done I appreciate it again

  • @Renancarvalho my solution works with php, java, or with any other language although it is not the most correct, it is necessary to create a routine within a while, something like: 1ºusa "while i < 6 do chamarotina(i); i = i+1"; 2nd in routine query and save values in a list. Try something like that, anything doubt or explanation a little more detailed warn.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.