在特定查询中的同一字段上排序结果集

时间:2013-04-23 06:55:09

标签: php mysql

我有一个看起来像

的功能
function myFunction($team1, $team2) {
$query = "select team_name from myteam where team_id=$team1 or team_id=$team2";
}

这样的结果总是由较低的team_id命令,所以如果它是

myFunction(12, 8);

返回将是team8然后是team12。我想要做的是按照查询列表中的第一个团队排序结果。

这可能吗?

3 个答案:

答案 0 :(得分:2)

function myFunction($team1, $team2) {
$query = "select team_name from myteam where team_id=$team1 or team_id=$team2 ORDER BY (team_id=$team1) DESC, team_id";
}

答案 1 :(得分:1)

SELECT team_name 
  FROM myteam 
 WHERE team_id IN ($team1,$team2)
 ORDER
    BY FIELD(team_id,$team1,$team2);

答案 2 :(得分:0)

试试这个,

 function myFunction($team1, $team2) {
 $query = "select team_name from myteam where (team_id=$team1 OR team_id=$team2) ORDER BY team_id DESC";
 }