I would like to select some information I have stored in my MySQL DB.
I have 2 tables named gameTable
and playerTable
. Now I want to get the gameTable
and get the names and levels of these players.
This is what I got so far, it works but I'm not sure this is the most efficient way. Do I really need to create a join for every referred row?
<?php
include ('connect.php');
$result = mysqli_query($connect,
"SELECT gameTable.id,
playerTable1.name AS name1, playerTable1.level AS level1,
playerTable2.name AS name2, playerTable2.level AS level2,
playerTable3.name AS name3, playerTable3.level AS level3,
playerTable4.name AS name4, playerTable4.level AS level4
FROM gameTable
JOIN playerTable AS playerTable1 ON gameTable.P1_id = playerTable1.id
JOIN playerTable AS playerTable2 ON gameTable.P2_id = playerTable2.id
JOIN playerTable AS playerTable3 ON gameTable.P3_id = playerTable3.id
JOIN playerTable AS playerTable4 ON gameTable.P4_id = playerTable4.id
");
while ($record = mysqli_fetch_array($result))
{
echo $record['id'] . " - "
. $record['name1'] . " - " . $record['level1'] . " - "
. $record['name2'] . " - " . $record['level2'] . " - "
. $record['name3'] . " - " . $record['level3'] . " - "
. $record['name4'] . " - " . $record['level4'] . "<br>";
}
?>
As a secondary question, my database is set to MyISAM. Would it be helpful to change it to InnoDB? Or is this only useful for update delete etc. In your database and not for fetching data?