我有一个用户表,其中有列ID。我需要获取ID列没有重复值的行。例如,这是我的表结构
ID-名称-电子邮件
1 --- xxx ------ xx
2 --- xxx ------- xx
2 --- xxx ------ xx
1 --- xxx ------- xx
public function allUsers(Request $request)
{
$this->updateOnlineStatus($request);
$query = UsersList::query();
$query->addSelect('users_list_view.user_type');
$scoreDate = $request->get('score_date');
if ($scoreDate &&
(trim($scoreDate['from']) !== ''
|| trim($scoreDate['to']) !== '')
) {
$query->selectSub(function ($q) use ($scoreDate) {
$q->select(DB::raw('AVG(fortune_scores.score)*20 AS score'))
->from('fortune_scores')
->leftJoin('fortunes', 'fortunes.id', '=', 'fortune_scores.fortune_id')
->whereRaw('fortunes.user_id = users_list_view.id')
->distinct('id')
->get();
if (trim($scoreDate['from']) !== '') {
$scoreDate['from'] = Carbon::createFromFormat('d/m/Y H:i:s', $scoreDate['from'] . ' 00:00:00');
$q->where('fortune_scores.created_at', '>=', $scoreDate['from'])
->distinct('id')
->get();
}
if (trim($scoreDate['to']) !== '') {
$scoreDate['to'] = Carbon::createFromFormat('d/m/Y H:i:s', $scoreDate['to'] . ' 23:59:59');
$q->where('fortune_scores.created_at', '<=', $scoreDate['to'])
->distinct('id')
->get();
}
return $q;
}, 'percentage');
$query->selectSub(function ($q) use ($scoreDate) {
$q->select(DB::raw('COUNT(1) AS count'))
->from('fortunes')
->whereRaw('fortunes.user_id = users_list_view.id')
->whereIn('fortunes.status', [FortuneStatus::COMMENT_SENT, FortuneStatus::PENDING_ANSWER, FortuneStatus::ANSWER_SENT])
->distinct('id')
->get();
if (trim($scoreDate['from']) !== '') {
$scoreDate['from'] = Carbon::createFromFormat('d/m/Y H:i:s', $scoreDate['from'] . ' 00:00:00');
$q->where('fortunes.updated_at', '>=', $scoreDate['from'])
->distinct('id')
->get();
}
if (trim($scoreDate['to']) !== '') {
$scoreDate['to'] = Carbon::createFromFormat('d/m/Y H:i:s', $scoreDate['to'] . ' 23:59:59');
$q->where('fortunes.updated_at', '<=', $scoreDate['to'])
->distinct('id')
->get();
}
}, 'fortune_count');
}
$dataTable = new DataTableService($query, collect($request->except('score_date')));
return $dataTable->setResultMap(function ($item) {
$statusClass = $item->online_status == 0 ? 'danger' : ($item->online_status == 1 ? 'yellow-lemon' : 'green-jungle');
$statusName = $item->online_status == 0 ? 'çevrimdışı' : ($item->online_status == 1 ? 'meşgul' : 'çevrimiçi');
return [
'id' => $item->id,
'name' => $item->name,
'online_status' => '<span class="label label-' . $statusClass . '">' . $statusName . '</span>',
'balance' => ($item->balance ? $item->balance : 0) . " TL",
'fortune_count' => ($item->user_type == UserType::RANDOM_TELLER ? User::find($item->id)->fortuneCount() : $item->fortune_count),
'percentage' => number_format($item->percentage, 2) ?: '0',
'credit' => $item->credit,
'last_login' => $item->last_login ? Carbon::createFromFormat('Y-m-d H:i:s', $item->last_login)->format('d.m.Y H:i:s') : '',
'action' => '<a href="' . route('admin.user.detail', ['id' => $item->id]) . '" class="btn btn-sm btn-outline grey-salsa" target="_blank"><i class="fa fa-search"></i> Detay</a>',
];
})->get();
}
我尝试“获取,区分,选择,计数”,但是无法处理我的代码吗?