【问题标题】:Can't get SQL query in laravel 5.1在 laravel 5.1 中无法获取 SQL 查询
【发布时间】:2016-03-09 12:26:52
【问题描述】:

如何在 laravel 5.1 中获取此查询。我有模型 Menu,数据库 menus。 SQL 中的这个查询得到我想要的结果。

SELECT b.title FROM menus a, menus b WHERE a.id=b.parent_id 

同样的问题,我无法得到这个查询

SELECT m.* FROM menus m WHERE m.id in (select m2.parent_id from menus m2)

【问题讨论】:

    标签: php sql laravel-5.1


    【解决方案1】:

    Laravel docs for queries 有一个你可以即兴发挥的例子:

    $users = DB::table('users')
                ->join('contacts', 'users.id', '=', 'contacts.user_id')
                ->join('orders', 'users.id', '=', 'orders.user_id')
                ->select('users.*', 'contacts.phone', 'orders.price')
                ->get();
    

    尝试使用类似的东西,注意可以use aliases

    $variable = DB::table('menus as a')
                ->join('menus as b', 'a.id', '=', 'b.parent_id')
                ->select('b.title')
                ->get();
    

    查看How to do this in Laravel, subquery where in 以获取与where in 等效的示例。

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2016-04-14
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      相关资源
      最近更新 更多