【问题标题】:Laravel DB Fasade for Join query with a union()Laravel DB Fasade 用于使用 union() 进行联接查询
【发布时间】:2017-09-15 13:09:40
【问题描述】:

当我设置了一个使用两个数据库的“.env”时,还编写了如下代码以供使用。

但是 where() 方法使用不正确。

你能告诉我更详细的使用 where() 方法的用法和解释或告诉我一些学习的链接吗?

感谢您。

$master = DB::connection('master_db')->table('customer_master')
            ->where(['name', '=', 'string'],
                    ['tel', '=', 'integer'],
                    ['address','=', 'string']);

$slave = DB::connection('slave_db')->table('customer_slave')
            ->where(['histories', '=', 'string'])
            ->union($master)
            ->get();

【问题讨论】:

  • 不确定你在问什么,虽然我以前从未见过 where() 使用的数组语法,但它看起来是正确的。您可以使用->toSql()->getBindings() 转储 SQL 和绑定以检查查询本身。

标签: database laravel laravel-facade


【解决方案1】:

这样写查询:

$master = DB::connection('master_db')->table('customer_master')
    ->where([
        'name' => 'string',
        'tel' => 'integer',
        'address' => 'string'
    ]);

$slave = DB::connection('slave_db')->table('customer_slave')
    ->where('histories', 'string')
    ->union($master)
    ->get();

这里的数组语法已针对$master 进行了调整,where() 已针对$slave 进行了调整。 = 比较是默认的,所以这里不需要指定。

【讨论】:

    猜你喜欢
    • 2014-02-25
    • 2012-05-06
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2018-10-04
    • 2011-12-20
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多