简体   繁体   中英

how to solve this mysql query

I have 3 mysql tables

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

Now I have to display leaderboard using php.

I tried this.

SELECT t_id FROM team;

got result.

then, in for loop

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.

`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? i want it in descending order.

There is no && in MySQL Query. Replace that with AND Operator on your query.

Since you want to get the data from the two tables, then JOIN the two tables instead of doing that with a loop:

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.

尝试这个

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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