我有两个两张桌子:
部门(部门)
id, dept_code和 dept_point
名称(名称)
id, dept_code, designation_point和 created_at
我想从指定中选择dept_code,designation_point和created_at。该查询应遍历指定表并使用这些条件。
条件
从dept_code不等于101和102的指定中选择所有dept_code,designation_point和created_at
如果在指定时dept_code = 101或dept_code = 101,则应使用以下条件从指定中选择匹配的dept_code,designation_point和created_at:
department.dept_code =名称.dept_code
department.dept_point = designation.designation_point
这是我已经拥有的代码
控制器
$designations = DB::table('designation as g')
->select(
'g.dept_code',
'g.designation_point',
DB::raw("DATE(created_at) as created_date")
)
->orderByRaw('g.created_at DESC')
->get();
查看
@foreach($designations as $key => $designation)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $designation->dept_code }}</td>
<td>{{ $designation->designation_point }}</td>
<td>{{ $designation->created_date }}</td>
</tr>
@endforeach
如何在Controller中完成代码
答案 0 :(得分:0)
我不确定您第二个条件是什么意思。我将相应地更新答案。
使用Builder
$designations = DB::table('designations')
->select('dept_code', 'designation_point', 'created_at')
# Condition 1
->whereNotIn('dept_code', [101, 102])
->orderBy('created_at', 'desc')
->get();
使用口才
$designations = App\Designation::select('dept_code', 'designation_point', 'created_at')
# Condition 1
->whereNotIn('dept_code', [101, 102])
->orderBy('created_at', 'desc')
->get();