简体   繁体   English

如何解决这个MySQL查询

[英]how to solve this mysql query

I have 3 mysql tables 我有3个mysql表

  • user(u_id(p),name) , user(u_id(p),name)
  • team(t_id(p),u_id(f),t_name,t_money,days_money) and team(t_id(p),u_id(f),t_name,t_money,days_money)
  • history(t_id(f),day,d_money) . history(t_id(f),day,d_money)

Now I have to display leaderboard using php. 现在,我必须使用php显示排行榜。

I tried this. 我试过了

SELECT t_id FROM team;

got result. 得到了结果。

then, in for loop 然后,在for循环中

foreach($tid_all as $tid)
{
     $que = $db_con->prepare("SELECT d_money, t_name FROM team, history WHERE t_id= '".$tid['t_id']."' && day='1'");

     $que->execute();

        while($info = $que->fetch(PDO::FETCH_NUM))
        {

                  echo "<tr>";
                  echo  "<td>".$info[0]."</td>";
                  echo  "<td>".$info[1]."</td>";
                  echo "</tr>";
         }
}

but it didnt work. 但是没有用。 any solution? 有什么办法吗?

Solution 1: i tried this and it worked. 解决方案1:我尝试过此方法,并且有效。

`SELECT d_money, t_name FROM team, history WHERE history.t_id=$tid['t_id'] AND team.t_id=history.t_id`

is it correct way or not? 这是正确的方法吗?

thanks everyone for help. 谢谢大家的帮助。

Question : is it possible to order the result table by d_money? 问题:可以按d_money排序结果表吗? i want it in descending order. 我要按降序排列。

There is no && in MySQL Query. MySQL查询中没有&& Replace that with AND Operator on your query. 在查询中将其替换为AND运算符。

Since you want to get the data from the two tables, then JOIN the two tables instead of doing that with a loop: 由于您要从两个表中获取数据,因此请JOIN两个表,而不是通过循环来完成:

SELECT
  h.d_money,
  t.t_name
FROM team AS t
INNER JOIN history AS h ON t.t_id = h.t_id;

Run this single query once and you will get what you want. 运行一次此查询,您将获得所需的内容。 You can also add a WHERE clause at the end of it the way you did in your query. 您也可以像在查询中一样在其末尾添加WHERE子句。

尝试这个

SELECT d_money, t_name FROM team, history WHERE team.t_id= '".$tid['t_id']."' AND history.t_id= '".$tid['t_id']."' && day='1'

AND替换&& AND尝试像这样:

"SELECT d_money, t_name FROM team, history WHERE t_id= '".$tid['t_id']."' AND day='1' order by d_money DESC "

Can you replace 你能代替吗

 WHERE t_id= '".$tid."' AND day='1'

instead of 代替

 WHERE t_id= '".$tid['t_id']."' && day='1'

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

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