如何链接和显示来自两个不同表的MySQL数据?

时间:2018-09-19 13:21:37

标签: php mysql ranking

我有两个名为“ stats”和“ users”的表

users表具有所有典型的用户数据,例如id,username,password,email(columns)
统计表中包含ID,攻击,防御,统计,黄金,食品(列)

我想并排显示这两个表中的数据,并通过其IDS链接数据 例如,

 Rank   user_uid   ostats     attack    defense    gold 
   1    Test        10          5         5        100
   2    Test2       8           2         6        60
   3    Test3       6           5         1        40

用户名来自表“ users”,其余用户来自表“ stats”

所以首先我想知道如何链接和显示来自同一ID的数据,例如Username(user_id = 1)和ostats,攻击,防御,黄金,food(id = 1)

然后我希望它们按其“状态”排序(我在任何表中都没有名为“等级”的列,只是不知道如何使用总体状态来创建等级)

2 个答案:

答案 0 :(得分:1)

您可以执行类似(未测试)的操作

SELECT u.username, s.overall, s.attack, s.defense, s.gold 
FROM stats s JOIN users u on s.user_uid = u.id 
ORDER BY s.overall;

可能的排名方法:

set @row_number=0;
SELECT (@row_number:=@row_number+1) as rank, u.username, s.overall, s.attack, s.defense, s.gold 
FROM stats s JOIN users u on s.user_uid = u.id 
ORDER BY s.overall;

另一种恐怖的尝试:

set @row_number = (select count(*) from users) + 1;
select (@row_number:=@row_number-1) as rank,  u.username, s.overall from 
stats s join users u on s.user_uid = u.id order by s.overall desc;
set @row_number = 0;

在PHP代码中,您必须将其作为两个查询运行以设置变量,然后运行实际的排名查询。这样,运行时rank变量始终设置为0。请注意,我使用了不同的表名和列名,只是为了简化一点。请记住要适应您的特定需求。

// connect to database
$conn = mysqli_connect("localhost", "user", "password", "database");
// this query will set a variable to 0.
$setSql = "SET @row_number = 0;";
// run the query. This will return a boolean - true or false, depending on whether or not the query ran successfully
$variableSet = mysqli_query($conn, $setSql);
// if the query ran successfully
if($variableSet){
    // setup the actual ranking query
    $statsSql = "select 
                     (@row_number:=@row_number+1) as rank,
                     u.id,
                     u.username,
                     s.overall
                 from
                     mstats s 
                 join 
                     musers u
                 on 
                     s.muser = u.id
                 order by 
                     s.overall desc;";
    $ranks = mysqli_query($conn, $statsSql);
    if(!$ranks){
        // dump error from rank query
        var_dump($conn->error); 
    } else {
        // dump results as associative array
        var_dump($ranks->fetch_all(MYSQLI_ASSOC));
    }
} else {
    // dump errors from setting variable
    var_dump($conn->error); 
}

对我来说,结果转储如下:

array (size=3)
  0 => 
    array (size=4)
      'rank' => string '1' (length=1)
      'id' => string '2' (length=1)
      'username' => string 'Bar' (length=3)
      'overall' => string '1000' (length=4)
  1 => 
    array (size=4)
      'rank' => string '2' (length=1)
      'id' => string '6' (length=1)
      'username' => string 'Tom' (length=3)
      'overall' => string '7' (length=1)
  2 => 
    array (size=4)
      'rank' => string '3' (length=1)
      'id' => string '1' (length=1)
      'username' => string 'Foo' (length=3)
      'overall' => string '3' (length=1)

答案 1 :(得分:0)

array(10) { [0]=> array(4) { ["rank"]=> string(1) "7" ["user_id"]=> string(1) "7" ["user_uid"]=> string(11) "Rubberguy12" ["ostats"]=> string(5) "90699" } [1]=> array(4) { ["rank"]=> string(1) "1" ["user_id"]=> string(1) "1" ["user_uid"]=> string(5) "Admin" ["ostats"]=> string(3) "351" } [2]=> array(4) { ["rank"]=> string(1) "2" ["user_id"]=> string(1) "2" ["user_uid"]=> string(13) "NotSoGodSpeed" ["ostats"]=> string(3) "330" } [3]=> array(4) { ["rank"]=> string(1) "8" ["user_id"]=> string(1) "8" ["user_uid"]=> string(3) "Ram" ["ostats"]=> string(2) "20" } [4]=> array(4) { ["rank"]=> string(1) "9" ["user_id"]=> string(1) "9" ["user_uid"]=> string(12) "TestAccount1" ["ostats"]=> string(2) "20" } [5]=> array(4) { ["rank"]=> string(2) "10" ["user_id"]=> string(2) "10" ["user_uid"]=> string(4) "Ajit" ["ostats"]=> string(2) "20" } [6]=> array(4) { ["rank"]=> string(1) "3" ["user_id"]=> string(1) "3" ["user_uid"]=> string(9) "codoriano" ["ostats"]=> string(1) "0" } [7]=> array(4) { ["rank"]=> string(1) "4" ["user_id"]=> string(1) "4" ["user_uid"]=> string(9) "Adminwsda" ["ostats"]=> string(1) "0" } [8]=> array(4) { ["rank"]=> string(1) "5" ["user_id"]=> string(1) "5" ["user_uid"]=> string(15) "NotSoGodSpeed12" ["ostats"]=> string(1) "0" } [9]=> array(4) { ["rank"]=> string(1) "6" ["user_id"]=> string(1) "6" ["user_uid"]=> string(16) "NotSoGodSpeed120" ["ostats"]=> string(1) "0" } }

在使用您的代码后显示,但通常数组从0开始,所以它从等级0到等级9,在这里我有10位用户在这里排名,我希望它从0开始(从1开始),那么如何从该数组中回显该特定编号(等级编号)?我尝试了echo $ranks[1];,但它给了我错误“致命错误:未捕获的错误:无法在

中使用mysqli_result类型的对象作为数组”

我尝试了while循环并获取assoc的常用方法

// this query will set a variable to 0.
$setSql = "SET @row_number = 0;";
// run the query. This will return a boolean - true or false, depending on whether or not the query ran successfully
$variableSet = mysqli_query($conn, $setSql);
// if the query ran successfully
if($variableSet){
    // setup the actual ranking query
    $statsSql = "select 
                     (@row_number:=@row_number+1) as rank,
                     u.user_id,
                     u.user_uid,
                     s.ostats
                 from
                     stats s 
                 join 
                     users u
                 on 
                     s.id = u.user_id
                 order by 
                     s.ostats desc;";
    $ranks = mysqli_query($conn, $statsSql);
    if(!$ranks){
        // dump error from rank query
        var_dump($conn->error); 
    } else {


        while ($row = mysqli_fetch_assoc($ranks)) {


       echo $row['rank'];

            echo "<br>";

        }


    }
} else {
    // dump errors from setting variable
    var_dump($conn->error); }


当我echo $row['rank'];时,它以DESC顺序(根据整体统计信息而不是实际排名)回应玩家的ID(来自用户的user_id)。

我尝试了echo $row[1];,但是它给我的错误是“注意:未定义的偏移量:1英寸”

那我在做什么错了?

我认为

出了点问题
"select (@row_number:=@row_number+1) as rank,"

因为那是我唯一不了解的部分,我是否需要用某些东西代替它们?