循环时将特定记录显示为最后一个值

时间:2013-12-06 05:58:41

标签: php mysql

表记录是

tag       enabled

paypal      1
bank        1
creditcard  1
COD         1

我的查询是

$sql = "SELECT * FROM payment_type WHERE enabled = '1'";
    $result = mysql_query($sql);
    while($data = mysql_fetch_assoc($result)) {
    echo $data['tag'];
}  

由于某些原因,我想把信用卡显示为最后一个值。提前谢谢。

2 个答案:

答案 0 :(得分:1)

使用case制作单独的排序顺序

SELECT * FROM payment_type 
WHERE enabled = '1'
order by case when tag = 'creditcard' 
              then 1
              else 2
         end

答案 1 :(得分:1)

试试这个:请参阅 DEMO

$sql = "SELECT * FROM payment_type WHERE enabled = '1'
        Order By case when tag <> 'creditcard' then 0 else 1 end;"