MPi*_*kle 19 php mysql sphinx laravel
我正在使用SphinxSearch查询一些内容并拥有我想用MySQL查询的对象的id.我的ID数组根据Sphinx给出的等级排序.因此,我想像这样制作一个MySQL:
SELECT * FROM table WHERE id IN (1,17,2)
ORDER BY FIELD(id,1,17,2)
Run Code Online (Sandbox Code Playgroud)
我知道我能做到:
Table::whereIn('id', $ids)->get();
Run Code Online (Sandbox Code Playgroud)
但我无法得到我的订单.
我怎么能用Laravel以适当的方式做到这一点?
MPi*_*kle 62
使用http://laravelsnippets.com/snippets/get-all-items-at-once-ordered-by-the-current-order-of-ids-in-the-where-in-clause-using上找到的解决方案-雄辩
$ids = array(1,17,2);
$ids_ordered = implode(',', $ids);
$items = static::whereIn('id', $ids)
->orderByRaw(DB::raw("FIELD(id, $ids_ordered)"))
->get();
Run Code Online (Sandbox Code Playgroud)
小智 6
我也遇到了这个问题,但我的目标数组元素是字符串。在这种情况下 ...
$strings = array('xxx','yyy','zzz');
$imploded_strings = implode("','", $strings);
$items = static::whereIn('some_column', $strings)
->orderByRaw(DB::raw("FIELD(some_column, '$imploded_strings')"))
->get();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12117 次 |
| 最近记录: |