Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP collect and combine different data from two tables

Tags:

php

mysqli

I have these two tables which I'm trying to make an output of. The first one **USERS** stores all information about a user, including an unique ID (androidID). The second one gets input based on number of laps a user has taken, and will make one row pr lap.

What I'm trying to do is to output the last entry of a given androidID in **ROUNDS**, whith the corresponding name etc. from **USERS**

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

This is how I'm quering the server

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

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

I tried to numerous combination of the following. With no luck. My PHP skills is not the best, I'm afraid.

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

I have really hit a wall trying to connect the tables.

like image 905
David Kristiansen Avatar asked Dec 18 '25 11:12

David Kristiansen


1 Answers

This would be the sql statement you want in your query.

SELECT * FROM `**USERS**` LEFT JOIN `**ROUNDS**`ON `**USERS**`.`androidID` = `**rounds**`.`androidID` ORDER BY `laps` desc 0,1;
like image 138
kayleighsdaddy Avatar answered Dec 21 '25 01:12

kayleighsdaddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!