在不应用任何顺序的情况下检索mysql数据

在不应用任何顺序的情况下检索mysql数据,mysql,zend-framework,socialengine,Mysql,Zend Framework,Socialengine,我有一个名为actions的表,主键为action\u id,我从这个表中检索数据,例如传递我自己的有序action\u id $actionIds = array(5,9,10,21,3,18,4); $actionsTb = Engine_Api::_()->getDbtable('actions','activity'); $postSelect = $actionsTb->select() ->where('action_id IN(?)'

我有一个名为actions的表,主键为action\u id,我从这个表中检索数据,例如传递我自己的有序action\u id

$actionIds = array(5,9,10,21,3,18,4);
$actionsTb = Engine_Api::_()->getDbtable('actions','activity');

$postSelect = $actionsTb->select()
              ->where('action_id IN(?)', $actionIds)
              ->where('type = ?', 'status')
             ;
现在的问题是,当我以升序返回结果时,如3,4,5,9,10,18,21,但我希望传递操作ID时的结果顺序意味着不希望对结果应用任何顺序。
请帮帮我。您也可以使用简单的查询进行回复。

既然您使用的是php,为什么不使用循环来动态创建代码中的where子句呢

$where = "action_id IN(";

for($x=0; $x<count($actionIds); $x++)
{

    // code for adding comma in every end of id 
    if($x==count($actionIds)-1)
    {
       // found the last data in array add closing parenthesis in IN funtion
       $where.=$actionIds[$x].")";

    }
    else
    {
       $where.=$actionIds[$x].",";
    }
}
这是完整的代码

$actionIds = array(5,9,10,21,3,18,4);
$where = "action_id IN(";

for($x=0; $x<count($actionIds); $x++)
{

    // code for adding comma in every end of id 
    if($x==count($actionIds)-1)
    {
       $where.=$actionIds[$x].",";
    }
    else
    {
      // add closing parenthesis in IN funtion
       $where.=$actionIds[$x].")";
    }
}

$actionsTb = Engine_Api::_()->getDbtable('actions','activity');

$postSelect = $actionsTb->select()
          ->where($where)
          ->where('type = ?', 'status')
         ;
我不知道zend编码,但下面的方法可能会对您有所帮助

->where( 'FIND_IN_SET( action_id, ? )', $actionIds )
我不确定zend's where是否将数组$ActionID转换为线性项值,即“5,9,10,21,3,18,4”。如果转换,结果查询的一部分将如下所示:

where find_in_set( action_id, '5,9,10,21,3,18,4' )
例如:

参考:
阅读

上的MySQL文档,谢谢CodeSlayer,我有订单或操作ID,但我想要的是,当我传递这些ID进行查询时,如果我没有传递任何订单,它将以ASC顺序返回数据,就像我有您所说的echo$where那样的ID顺序//因此,我认为结果是5,9,10,21,3,18,4中的action_id,我希望返回这些id的顺序…但它将返回顺序ASC,如3,4,5,10。。为什么不按操作添加查询order?默认情况下,它返回ASC order,但我需要order,因为我在ids数组中传递了ID。在mysql中,如果您不传递任何订单,它将自动为您提供ASC订单。我不想应用任何顺序,我只想对结果进行排序,因为我已将Ids数组传递给它。我认为在mysqlOne中不可能实现可能的解决方案是,我运行一个循环到我正在传递的Ids,并逐个获取结果,然后将这些结果逐个附加到数组中,但它会向服务器发送大量请求我只想按照传递ID的顺序获取数据,就像我按照3,6,2,8,7的顺序传递查询ID一样,它会以ASC的顺序给出结果……我想要数组中的ID的顺序……你可以在简单的查询中告诉我,我将自己构建zend query。
where find_in_set( action_id, '5,9,10,21,3,18,4' )
mysql> select find_in_set( 18, '5,9,10,21,3,18,4' );
+---------------------------------------+
| find_in_set( 18, '5,9,10,21,3,18,4' ) |
+---------------------------------------+
|                                     6 |
+---------------------------------------+
1 row in set (0.00 sec)