Home  >  Q&A  >  body text

php - laravel 的 Eloquent 设置排序的时候怎么以两个字段的差来排序?

Product::where([])->orderBy('num','desc');
Product::where([])->orderBy('click','desc');

我需要 类似以“click-num”排序

PHP中文网PHP中文网2682 days ago376

reply all(2)I'll reply

  • 阿神

    阿神2017-04-11 10:30:48

    你可以使用orderByRaw(string $sql, array $bindings = array())方法,直接把排序的sql写在第一个参数就可以了

    reply
    0
  • 怪我咯

    怪我咯2017-04-11 10:30:48

    -- mysql原語句
    select (click-num) click_num from table order by click_num desc
    // laravel寫法,如果ORM,則自行調整
    DB::table('table)->selectRaw('(click-num) as click_num')->orderBy('click_num', 'desc')->get()

    如果數據量大,以上性能會不佳,這塊自測吧!

    reply
    0
  • Cancelreply