Zend DB Select:ORDER BY FIELD('id',some_array) - 怎么样?

时间:2011-07-07 14:15:38

标签: mysql zend-framework zend-db

您如何在Zend框架中编写以下查询?

SELECT * FROM table_name ORDER BY FIELD(field_name, 'Small','Medium','Large');

我只需要“Order by”部分:)

谢谢!

3 个答案:

答案 0 :(得分:26)

这个怎么样:

      $db = Zend_Db_Table::getDefaultAdapter();

      $select = $db->select();

      $select->from('table_name')
              ->order(new Zend_Db_Expr("FIELD(field_name, 'Small','Medium','Large')"));


      var_dump($select->assemble());

结果:

string 'SELECT `table_name`.* FROM `table_name` ORDER BY FIELD(field_name, 'Small','Medium','Large')' (length=92)

答案 1 :(得分:1)

$select->order(new Zend_Db_Expr('FIELD(field_name, 'Small','Medium','Large')'));

答案 2 :(得分:1)

我认为你应该这样做:

$db = Zend_Db::factory( ...options... );
$select = $db->select()
 ->from(table_name)
 ->order(new Zend_Db_Expr("FIELD(field_name, 'Small','Medium','Large')")));