带有连接和参数分组的Laravel Query Builder错误

时间:2018-06-11 15:38:45

标签: laravel eloquent laravel-query-builder

有一组查询我想从原始转换为查询构建器,但在使用参数分组时遇到问题:

  

SQLSTATE [42S22]:找不到列:1054未知列   ' users.trail_ends_at'在' where子句' (SQL:选择'广告系列'。*   来自'用户'内部加入'广告系列'用户'。' id' =   '活动'' USER_ID'内部联接'订阅'在'用户'。' id' =   '订阅'' USER_ID'其中('用户'。' trial_ends_at> = 2018-06-15   00:00:00或'用户'。' trail_ends_at'是空的)和'广告系列'。'活跃' =   1和'广告系列'。' style' =每月和   ('订阅'。" trial_ends_at'> = 2018-06-15 00:00:00或   #&39;订阅'' trail_ends_at'是null)和   ('订阅'。' ends_at'> = 2018-06-15 00:00:00或   #&39;订阅'' ends_at'是null))

有效的SQL查询:

DB::select('select c.* 
FROM  users u, subscriptions s, campaigns c
WHERE   c.user_id = u.id
AND s.user_id = u.id
AND (u.trial_ends_at IS NULL OR u.trial_ends_at >= CURDATE() + :leadDays1)
AND c.active > 0
AND c.style = \'monthly\'
AND (s.ends_at IS NULL OR s.ends_at >= CURDATE() + :leadDays2)
AND (s.trial_ends_at IS NULL OR s.trial_ends_at >= CURDATE() + :leadDays3)
AND c.send_on <= CURDATE() + :leadDays4
AND MONTH(c.send_on) = MONTH(CURDATE() + :leadDays5),
AND DAYOFMONTH(c.send_on) = DAYOFMONTH(CURDATE() + :leadDays6)',
     [
       'leadDays1' => $leadDays,
       'leadDays2' => $leadDays,
       'leadDays3' => $leadDays,
       'leadDays4' => $leadDays,
       'leadDays5' => $leadDays,
       'leadDays6' => $leadDays
      ]
 );

Builder查询错误:

$checkDate = Carbon::today()->addDays($leadDays);

    return $campaigns = DB::table('users')
    ->join('campaigns', 'users.id', '=', 'campaigns.user_id')
    ->join('subscriptions', 'users.id', '=', 'subscriptions.user_id')
    ->select('campaigns.*')
    ->where(function ($query) use ($checkDate) {
        return $query->where('users.trial_ends_at', '>=', $checkDate)
        ->orWhereNull('users.trail_ends_at');
      })
    ->where('campaign.active', '=', 1)
    ->where( 'campaign.style', '=', $style)
    ->where(function ($query) use ($checkDate) 
        {
        return $query->where('subscriptions.trial_ends_at', '>=', $checkDate)
    ->orWhereNull('subscriptions.trail_ends_at');
       })
    ->where(function ($query) use ($checkDate) {
        return $query->where('subscriptions.ends_at', '>=', $checkDate)
    ->orWhereNull('subscriptions.ends_at');
       });

1 个答案:

答案 0 :(得分:1)

检查您的列名称。我认为trail_ends_at应为trial_ends_at