Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent - join table on itself

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)

like image 367
DaVluuu Avatar asked Apr 30 '26 18:04

DaVluuu


1 Answers

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();
like image 183
thefallen Avatar answered May 02 '26 23:05

thefallen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!