按自定义数组对行排序

ybzsozfc  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(376)

是否可以按自定义数组对行进行排序 ids 从另一张table或者其他什么地方给的。

$str = "5,2,3,6,911,18,118,65,985,15...";  

$arr = explode(',', $str);

function get_titles($status){
    global $db;
    $sql = "select * from arts where status = :astatus order by " . $arr . " asc";
    $st = $db->prepare($sql);
    $st->execute([":astatus" => $status]);
    ...
}
wj8zmpe1

wj8zmpe11#

如果你想定制的话 id 订单。。。


# $str = "5,2,3,6,911,18,118,65,985,15...";

# Values separated by comma

# It can be from array

# $str = implode(",",[5,2,3,6,911,18,118,65,985,15])

$str = "5,2,3,6,911,18,118,65,985,15";  

function get_titles($status,$arr){
    global $db;
    $sql = "SELECT * FROM arts WHERE status = :astatus ORDER BY FIELD(id,".$arr .") ASC";
    $st = $db->prepare($sql);
    $st->execute([":astatus" => $status]);
    ...
}

get_titles($status,$str)

相关问题