Laravel'加入',其中两个值在彼此的'x'范围内

时间:2016-10-06 04:25:34

标签: laravel-5 eloquent

我有一个MySQL查询,当我们彼此+/-'3'时,我加入两个值:

...
inner join 
on ABS(i.direction - i2.MWD) < 3 
and ABS(i.direction - i2.MWD) < 3

(我不完全确定为什么'和'陈述完全一样,但这就是它的作用)

我尝试使用

将其转换为Eloquent
$join->on('i.direction', '-', 'i2.MWD', '<' 3)

$join->on('i.direction', '=', 'i2.MWD')
  ->where('i.direction', '-', 'i2.MWD', '<' 3)

以上两个例子给我一个空洞的结果。如果我更改'&lt;'到'&gt;'在任何一个例子中,我从两个表中获得所有连接的数据。

我也尝试使用DB :: raw作为解决方法:

$join->on(DB::raw('ABS(input.direction - i2.MWD) < 3 
and ABS(input.direction - i2.MWD) < 3'));

给出了以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'on clause' 
(SQL: select count(*) as aggregate 
from (select * from `input` 
inner join 
 (select MWD, SWH, SWP, BUOY,Time from stations order by Time desc limit 4) 
  as i2 on ABS(input.direction - i2.MWD) < 3 
  and ABS(input.direction - i2.MWD) < 3 = ``) count_row_table)

('i2.MWD之后插入的尾随='') 3'令我感到困惑。)

如果有人能帮我提出一个加入i.direction和amp;的两个价值观的解决方案,我将非常感激。 i2.MWD(正负3)使用Eloquent。

1 个答案:

答案 0 :(得分:0)

你在这里尝试做什么:

->where('i.direction', '-', 'i2.MWD', '<' 3)

你必须改变这样的原始:

->whereRaw('i.direction - i2.MWD < ?', [3])

这应该有帮助。