【发布时间】:2021-12-28 14:45:45
【问题描述】:
这个返回来自这个查询。
$getRanking = DB::table('users')
->select(DB::raw('totalTime ,sum(userPoints)+sum(extraPoints) as TotalPoints, id, name, email'))
->groupby('id','name', 'email','totalTime')
->orderby('TotalPoints','desc','totalTime','desc')
->limit(10)
->get();
让我们理解我的疑问,我有这个数据。
{
"sucesso": true,
"data": [
{
"totalTime": "00:08:00",
"TotalPoints": "120",
"id": 1,
"name": "Randomname",
"email": "ddddd@c.com",
"empresa": "c"
},
{
"totalTime": "00:05:00",
"TotalPoints": "120",
"id": 2,
"name": "Randomname",
"email": "aaaa@gmail.com",
"empresa": "gmail"
}
}
当我有相等的点时,数据 0 是 120,数据 1 是 120。 在这种情况下,我需要显示总时间较少的第一个数据:
{
"sucesso": true,
"data": [
{
"totalTime": "00:05:00",
"TotalPoints": "120",
"id": 2,
"name": "Randomname",
"email": "aaaa@gmail.com",
"empresa": "gmail"
},
{
"totalTime": "00:08:00",
"TotalPoints": "120",
"id": 1,
"name": "Randomname",
"email": "ddddd@c.com",
"empresa": "c"
}
}
我可以只使用 SQL 来管理它还是需要使用 PHP 来订购?
【问题讨论】:
-
'desc'表示从高到低的降序排列,'asc'是从低到高的相反顺序。所以你需要把你的orderby改成升序对totalTime进行排序 -
我这样做了,但没有任何反应,钢相同的输出