我需要做以下选择。
我有一个具有不同状态(1 - 4)的100.000用户列表。我需要从中选择10.000用户,首先是状态4(但是如果状态4小于10.000而不是选择状态为3而不是2而不是1)
非常感谢帮助。
感谢您的回复,我试着选择Gordons版本。由于我需要结合,我现在有以下内容。但这只允许我优先考虑第二个选择的分数(%@ test2.com),但我需要为我创建的每个选项。如果我把"命令放在"在联盟之前,我收到了无效的语法通知:
SELECT TOP 10000 *
FROM [table]
WHERE Email like '%@test1.com' and score in ( 1, 2, 3, 4)
UNION
SELECT TOP 5000 *
FROM [table]
WHERE Email like '%@test2.com' and score in ( 1, 2, 3, 4)
order by score desc

答案 0 :(得分:2)
这是优先级查询。您可以使用ANSI标准函数function getHours($project_id, $project_startDate, $project_endDate)
{
$project = Project::find($project_id);
$users = $project->users()->get();
$start_date = $project_startDate;
$end_date = $project_endDate;
foreach ($users as $user) {
$timesheetHours = $user->timesheets()->whereBetween('date', [$start_date, $end_date])->get()->sum('hours');
}
return $timesheetHours;
}
处理它:
row_number()
您还可以将其简化为:
select t.*
from (select t.*,
row_number() over (order by status desc) as seqnum
from t
where status in (1, 2, 3, 4)
) t
where seqnum <= 10000;