【问题标题】:Laravel Query builder equivalent to SQL query?Laravel Query builder 相当于 SQL 查询?
【发布时间】:2018-05-02 02:02:40
【问题描述】:

我有两个表 Country 和 state,State 的 SQL 查询是

select state.id,state.state_name,Country.Country_name from country,state where state.country=country.id

这个查询工作正常,我想把它转换成 laravel 查询生成器

【问题讨论】:

  • 到目前为止你尝试了什么?
  • @JonasStaudenmeir 不懂语法

标签: laravel laravel-5.5 laravel-query-builder


【解决方案1】:

使用这个:

DB::table('country')
    ->select('state.id', 'state.state_name', 'country.country_name')
    ->join('state', 'state.country', '=', 'country.id')
    ->get();

【讨论】:

  • XX 和 YY 的表名, DB::table('xx') , join('yy',
  • 在这个查询中countrystate可以互换,没有区别。
【解决方案2】:

在控制器功能中:

DB::table('country')->leftjoin('state', function ($join) {
            $join('state', 'state.country', '=', 'country.id');})->get();

在控制器中导入数据库外观:

use Illuminate\Support\Facades\DB;

【讨论】:

    猜你喜欢
    • 2019-04-19
    • 2014-04-10
    • 1970-01-01
    • 2021-02-04
    • 2015-10-29
    • 2019-10-31
    • 1970-01-01
    • 2021-08-20
    • 2018-08-25
    相关资源
    最近更新 更多