duan7007 2017-07-17 10:57
浏览 6
已采纳

如何使用PHP和MySQL将固定位置的行值添加到数组中

I need to keep one row value from table in bottom position in an array using PHP and MySQL. Here is my table:

db_designation:

id      name

1       aaa

2       bbbb

3       Other

4       tttt

My query is below:

$sqldeg=mysqli_query($connect,"select * from db_designation order by id desc");
while ($row=mysqli_fetch_array($sqldeg)) {
            $desdata[]=array("did"=>$row['id'],"dname"=>$row['name']);
}

Here I need this id->3 name->Other row from table value always come in last in the array i.e-$desdata.

  • 写回答

2条回答 默认 最新

  • dourui9570 2017-07-17 11:04
    关注

    You could do it at query, with ordering before getting to php with.

    By id:

    ORDER BY (CASE WHEN `id`=3 THEN 1 ELSE 0 END) ASC, `id` DESC
    

    Or by name:

    ORDER BY (CASE WHEN `name`='Other' THEN 1 ELSE 0 END) ASC, `id` DESC
    

    this will append 1 when id or name matches, otherwise zero (0), and will order in ASCENDING order.

    So your query would be:

    $sqldeg=mysqli_query($connect,"select * from db_designation ORDER BY (CASE WHEN `id`=3 THEN 1 ELSE 0 END) ASC, `id` DESC");
    while ($row=mysqli_fetch_array($sqldeg)) {
                $desdata[]=array("did"=>$row['id'],"dname"=>$row['name']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起