mysql - Eloquent - 自己连接表

标签 mysql laravel eloquent

我正在尝试自己连接一个表,但总是出错。这是我当前的代码。我也尝试过 Raw 语句。目前不确定该往哪个方向走。

我的代码:

$Calle = db('VoipBill')
                    ->table('billing')
                    ->join('billing', 'billing.srcnum', '=', 'billing.dstnum')
                    ->where('acct_name', '100080_company')
                    ->where('srcnum', $call->srcnum)
                    ->whereBetween('calldate', [$set_time_lower, $set_time_upper])
                    ->get();

这是错误:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'billing' (SQL: select * from billing inner join billing on billing.srcnum = billing.dstnum where acct_name = 100080_company and srcnum = +27******** and calldate between 2016-05-02 09:19:27 and 2016-05-02 09:19:37)

最佳答案

您必须为表设置别名。以这种方式尝试:

    $Calle = db('VoipBill')
        ->table('billing as bsrc')
        ->join('billing as bdst', 'bsrc.srcnum', '=', 'bdst.dstnum')
        ->where('bsrc.acct_name', '100080_company')
        ->where('bsrc.srcnum', $call->srcnum)
        ->whereBetween('bsrc.calldate', [$set_time_lower, $set_time_upper])
        ->get();

关于mysql - Eloquent - 自己连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38198108/

相关文章:

mysql - 如何使用 order by 和 limit 获取多行

php - 我确保数据库有图像并且数据库连接正确,但我无法显示图像

php - 显示 BLOB 图片 Laravel 4

php - 从数据库中删除打印结果的最佳方法

php - 在 laravel 中访问动态数据库

php - 使用 Laravel Eloquent 在 MySQL 列别名中添加空格

mysql - LIKE '[charlist]%' 语法在 MySQL 中不起作用 (phpMyAdmin)

mysql - MY sql 查询查找 # 状态

namespaces - Laravel - Eloquent : Polymorphic relations with namespace

php - 如何限制从 Eloquent 查询中检索的信息并阻止其崩溃?