Eloquent - join table on itself

喜夏-厌秋 提交于 2021-01-28 18:29:43

问题


I'm trying to join a table on itself but keep getting errors. Here is my current code. I've also tried Raw statement. Not sure which direction to go in at the moment.

My code:

$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();

This is the error:

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)


回答1:


You have to alias the tables. Try it it this way:

    $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();


来源:https://stackoverflow.com/questions/38198108/eloquent-join-table-on-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!