duanjing1276 2012-06-27 18:17
浏览 54
已采纳

在SQL中排序WHERE [重复]

Possible Duplicate:
mysql custom sort

I have an array of IDs:

$ids = array(5, 1, 4, 2, 3);

and I make a query:

SELECT * FROM Persons
WHERE id IN $ids

and I receive this object:

1, 2, 3, 4, 5

Is it possible to sort this same as in array? I would like receive

5, 1, 4, 2, 3.

I also use PROPEL. Maybe with Propel ORM in Symfony, is this possible?

  • 写回答

2条回答 默认 最新

  • dongmisa4779 2012-06-27 18:28
    关注

    in mysql you can use the field function for custom sort order. For example:

    $ids = array(5, 1, 4, 2, 3);
    $ids = implode(',', $ids);
    $sql = "SELECT * FROM Persons
            WHERE id IN ($ids)
            ORDER BY FIELD(id, $ids)";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,