PHP / MYSQL - “ORDER BY”不起作用

时间:2013-08-23 15:51:54

标签: php mysql

我有一张表,其中有一堆用户拥有一定数量的积分。我想安排用户从最高点到最低点。但是ORDER BY PTS DESC不起作用。

                    <tr>
                        <th id="users_th1"><img src="<?php echo mysql_result($r_TEAMS, $i, 'LOGO'); ?>"/> <p><?php echo mysql_result($r_TEAMS, $i, 'NAME'); ?></p></th>
                        <th id="users_th2">Points Value</th>
                    </tr>
                    <?php 
                    $q_users = 'Select * from POINTS LEFT JOIN USERS on USERS.UID = POINTS.UID where TID = '.mysql_result($r_TEAMS, $i, 'TID');
                    $r_users = mysql_query($q_users, $connection) or die(mysql_error());
                    $n_users = mysql_num_rows($r_users);

                        for($k = 0; $k <$n_users; $k++){    
                    ?>



                    <tr>
                        <td class="person"><?php echo mysql_result($r_users, $k, 'NAME'); ?></td>
                        <td><?php echo mysql_result($r_users, $k, 'POINTS.PTS'); ?></td>
                    </tr>

                    <?php
                        }
                    }

2 个答案:

答案 0 :(得分:1)

这只是猜测,但我看到你在表USER和表POINTS之间进行了JOIN。也许您在两个表中都有一个名为PTS的字段,因此如果您想按该字段对结果进行排序,则应指明您所引用的表所属的表。

所以,这样做,

$q_users = "
    SELECT *
    FROM POINTS
    LEFT JOIN USERS
    ON USERS.UID = POINTS.UID
    WHERE <table name>.TID = " . mysql_result($r_TEAMS, $i, 'TID') . "
    ORDER BY <table name>.PTS DESC";

答案 1 :(得分:0)

你有没有尝试过:

$q_users = 'Select * from POINTS LEFT JOIN USERS on USERS.UID = POINTS.UID where TID = '.mysql_result($r_TEAMS, $i, 'TID').' ORDER BY PTS DESC;';

如果不排序或者不查询,你也可以写。