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

q9yhzks0  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(348)

我有两个名为“stats”和“users”的表
users表包含所有典型的用户数据,如id、用户名、密码、电子邮件(列)
统计表有id,攻击,防御,奥斯塔,黄金,食物(列)
我想并排显示这两个表中的数据,并通过它们的ID链接数据,例如,

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,attack,defence,gold,food(id=1)
然后我想让他们按“ostats”排序(我还没有在任何表中命名为“rank”的列,只是不知道如何使用总体统计创建rank)

oxcyiej7

oxcyiej71#

你可以这样做(未经测试)

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)

相关问题