在laravel中使用子查询中的count编写where子句

时间:2018-05-04 08:24:37

标签: mysql laravel count eloquent subquery

我想在Laravel中使用eloquent编写此查询。

<script src="https://unpkg.com/vue@2.5.16"></script>

<div id="app">
  <p draggable>Drag this text over the list items below</p>
  <ul>
    <li v-for="group in grouplist"
        :key="group.id"
        @dragenter="onDragEnter"
        @dragleave="onDragLeave">{{group.text}}</li>
  </ul>
</div>

我该怎么写呢?

我用select * from tbl_lawyers as l where ( select count(*) from cooprations as c where c.fk_lawyer_id = l.lawyer_id and c.fk_coopration_id in ($request->cooprations))>= count($request->cooprations); 写了它,但这不起作用。

>=

1 个答案:

答案 0 :(得分:0)

试试这个:

->where(DB::raw(count($request->cooprations)), '<=', function ($d) use ($request) {
    $d->selectRaw('count(*)')
        ->from('cooprations')
        ->whereIn('tbl_lawyer_cooperation.fk_cooperation_id', $request->cooprations);
});

原始表达并不理想,但它有效。我不确定有更好的方法来做到这一点。