PHP collects and combines different data from two tables

I have these two tables that I am trying to do. The first one **USERS**

stores all information about the user, including a unique identifier (androidID). The second gets an input based on the number of laps the user has traveled and will make one line pr lap.

What I am trying to do is output the last entry of a given androidID to **ROUNDS**

, with the appropriate name, etc. of**USERS**

_____________________        _________________________
|    **USERS**      |        |      **ROUNDS**       |
---------------------        -------------------------
| NAME              |        | ID(unique)            |
| LASTNAME          |        | TIME                  |
| androidID(unique) | <----> | androidID(NOT unique) |
| ...               |        | ROUNDS                |

      

This is how I access the server

$result_users = $con->query(
    "SELECT * FROM users"
);

$result_rounds = $con->query(
    "SELECT * FROM Rounds ORDER BY laps desc"
);

      

I have tried numerous combinations. No luck. I am afraid my PHP skills are not the best.

foreach ($result_users as $row_users) {
    foreach ($result_rounds as $row_rounds) {
         if($row_users['androidID'] == $row_rounds['androidID'] {
             // Do some wizzardy
         }
    }
}

      

I actually hit the wall while trying to put the tables together.

+3


source to share


1 answer


This will be the sql query you want in your query.



SELECT * FROM `**USERS**` LEFT JOIN `**ROUNDS**`ON `**USERS**`.`androidID` = `**rounds**`.`androidID` ORDER BY `laps` desc 0,1;

      

+1


source







All Articles