2014-04-01 24 views
0

我有一個簡單的查詢如何使用psudo列進行ORDER BY?

$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?

+1

可能重複[MySQL排序計算](http://stackoverflow.com/questions/2369624/mysql-sort-on-a-calculation) – jeroen

+1

如果點不等於1,$ cooef的值是多少或2? – mesutozer

+0

爲了模仿原本使用0的代碼(如未定義,並且拋出警告),您需要在查詢中添加一個額外的'IF()' - 請參閱下面的答案。 –

回答

4

您可以在您的查詢和訂單計算pointsf:

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

什麼時候積分不是2而不是1.5?舊代碼將使用0(並引發警告)。 –

+0

我意識到,發佈答案後,現在問@Aasim Azam應該是什麼行爲 – mesutozer

+0

這些是唯一的兩個數字,沒有其他人,在數據庫和乘法中,這意味着永遠不會有一個零 –

0

當然:-)用途:

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

大概$ cooef爲0時,$點不是1或2

相關問題