Eloquent - 连接表本身 [英] Eloquent - join table on itself
问题描述
我正在尝试自行加入一个表,但一直出现错误.这是我当前的代码.我也试过 Raw 语句.目前不知道该往哪个方向发展.
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.
我的代码:
$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]:语法错误或访问冲突:1066 不是唯一的表/别名:'billing'(SQL:select * from billing
内连接billing
on billing
.srcnum
= billing
.dstnum
where acct_name代码>= 100080_company 和
srcnum
= +27******** 和 calldate
在 2016-05-02 09:19:27 和 2016-05-02 09 之间:19:37)
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'billing' (SQL: select * from
billing
inner joinbilling
onbilling
.srcnum
=billing
.dstnum
whereacct_name
= 100080_company andsrcnum
= +27******** andcalldate
between 2016-05-02 09:19:27 and 2016-05-02 09:19:37)
推荐答案
您必须为表设置别名.试试这个方法:
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();
这篇关于Eloquent - 连接表本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!