简体   繁体   English

获取多个表MySQL,加入

[英]Fetch multiple tables MySQL, join

I would like to select some information I have stored in my MySQL DB. 我想选择一些存储在MySQL数据库中的信息。 For example I have 2 tables gameTable and playerTable. 例如,我有2个表gameTable和playerTable。 Now I want to get the gameTable and get the names and levels of these players. 现在,我想获取gameTable并获取这些玩家的名称和级别。 How do I do this?? 我该怎么做呢??

在此处输入图片说明

This is what i got so far, it works but I'm not sure this is the most efficient way. 这是我到目前为止获得的结果,它可以工作,但是我不确定这是最有效的方法。 Do I need to create a join for every reffered 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>";
    }

?>

A second question, my database is set to MyISAM. 第二个问题,我的数据库设置为MyISAM。 Would it be helpful to change it to InnoDB? 将其更改为InnoDB是否有帮助? Or is this only useful for update delete etc. In your database and not for fetching data? 或者这仅对更新删除等有用。在您的数据库中而不对获取数据有用吗?

I would do it like this with the string(in reference to the comment question, not to the original question). 我会这样处理字符串(参考注释问题,而不是原始问题)。 This reserves just one string space for each location in memory. 这仅为内存中的每个位置保留一个字符串空间。 This saves the overhead of keeping assigning values for the intermediate strings and should save memory and even make execution time faster because string concatenation is a dreadfully slow processs at times. 这节省了为中间字符串分配值的开销,并且应该节省内存,甚至可以加快执行时间,因为有时字符串连接是一个非常缓慢的过程。

$myarr = array();
 while ($record = mysqli_fetch_array($result))
  { 
    $myarr[] = $record['id'] ; 
    $myarr[] = " - " ;
    $myarr[] = $record['name1'] ;
    $myarr[] = " - " ;
    $myarr[] = $record['level1'];
    $myarr[] = " - ";
    $myarr[] = $record['name2'];
    $myarr[] = " - ";
    $myarr[] = $record['level2'];
    $myarr[] = " - ";
    $myarr[] = $record['name3'];
    $myarr[] = " - ";
    $myarr[] = $record['level3'];
    $myarr[] = " - ";
    $myarr[] = $record['name4'];
    $myarr[] = " - ";
    $myarr[] = $record['level4'];
    $myarr[] = "<br>";
 }
 echo implode('',$myarr);
 unset($myarr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM