【发布时间】:2019-03-15 01:10:14
【问题描述】:
我得到这样的 sql 查询:
select * from
geofilter_activationswhere not exists (select * fromsnap_submissionswheregeofilter_activations.purchase_id=snap_submissions.purchase_id) andstatus= 0 andcreated_atid desc limit 1
但是我添加了 wherenotnull 的 subscription_id 的查询发生了什么? 它没有出现在查询输出中。给我一个方法来优化查询并让它工作。
$time_before = '10';
$dt = Carbon::now();
$activation = GeofilterActivation::doesntHave('submission_data')
->where('status', '0')
->where('created_at', '<', $dt->subMinutes($time_before)->toDateTimeString())
->OrderBy('id','desc')
->first();
$activation->load(['purchase' => function ($query) {
$query->whereNotNull('subscription_id');
}]);
GeoFilterActivation 模型是:
<?php
namespace App\Models\Geofilter;
use Illuminate\Database\Eloquent\Model;
class GeofilterActivation extends Model
{
public function purchase(){
return $this->belongsTo('App\Models\Geofilter\GeofilterPurchase', 'purchase_id');
}
public function submission_data(){
return $this->belongsTo('App\Models\Geofilter\SnapSubmission', 'purchase_id', 'purchase_id');
}
public function ad_stat(){
return $this->belongsTo('App\Models\Geofilter\SnapAdStat', 'purchase_id', 'purchase_id');
}
public function currency(){
return $this->belongsTo('App\Models\Currency', 'currency_code', 'code');
}
}
【问题讨论】:
-
你是如何“获取 sql 查询”的?
标签: mysql laravel laravel-5 eloquent