如何使用伪列进行ORDER BY?

时间:2014-04-01 21:54:25

标签: php mysql

我有一个简单的查询

$query = SELECT * FROM 'tablename';

我接受查询并通过while循环

$id = $row['id'];
$player = $row['player'];
$club = $row['club'];
$games = $row['games'];
$goals = $row['goals'];
$points = $row['points'];

if($points == 2){$cooef = "2";}
elseif($points == 1){$cooef = "1.5";}

    $pointsf = $games == 0 ? 0 : ($cooef*$goals);

        $showesm .= "
        <div style='$style' class='position'>" . $count . "</div>
        <div style='$style' class='recordplayer'>" . $player . "</div>
        <div style='$style' class='playerclub'>" .  $club . "</div>
        <div style='$style' class='position'>" . $games . "</div>
        <div style='$style' class='position'>" . $goals . "</div>
        <div style='$style' class='position'>" . $pointsf . "pts</div>
            ";
}

我希望$pointsf列对结果输出进行排序。这可能在php或MYSQL中吗?

2 个答案:

答案 0 :(得分:4)

您可以通过以下方式计算查询和订单中的pointsf:

$query = "SELECT *, IF(games=0,0,IF(points = 2, 2*goals, 1.5*goals)) AS pointsf
          FROM 'tablename' ORDER BY pointsf"

答案 1 :(得分:0)

当然:-)使用:

$query = SELECT * FROM 'tablename' ORDER BY IF(games = 0, 0, goals * IF(points = 2, 2, IF(points = 1, 1.5, 0)));

当$ points不是1或2时,大概$ cooef为0