MYSQL 在分组前排序
MYSQL to order before grouping by
我有以下内容:
user_id date_created project_id
3 10/10/2013 1
3 09/10/2013 1
5 10/10/2013 1
8 10/10/2013 1
10 10/10/2013 1
3 08/10/2013 1
我想要的最终结果是:
user_id date_created project_id
3 10/10/2013 1
5 10/10/2013 1
8 10/10/2013 1
10 10/10/2013 1
上下文:
我有一个叫做影响力的东西,一个用户可以对一个项目有很多影响力。
我想获取用户对项目的最新影响列表。
我试过:
select * from influences
where project_id = 1
group by user_id
ORDER BY created_at DESC
但是当然这忽略了首先由创建于 的用户排序,然后对完整列表进行排序。它只是简单地将用户挤压在一起并订购最终列表
LARAVEL - Eloquent 提供的答案是这样的:
return Influence::select( "user_id", "influence", DB::raw( "MAX(created_at) as created_at" ) )
->where( "project_id", "=", $projectID )
->groupBy( "user_id", "project_id" )->get();
您不想在 group by
之前订购,因为给定您的查询结构,它没有必要按照您的意愿进行。
如果您想要最近创建的影响力,请明确获取它:
select i.*
from influences i join
(select user_id, max(created_at) as maxca
from influences i
where project_id = 1
group by user_id
) iu
on iu.user_id = i.user_id and iu.maxca = i.created_at
where i.project_id = 1;
您的意图是使用文档 明确 警告不要使用的 MySQL 扩展。您想要包含 select
中不在 group by
中的列。正如 documentation 所说:
MySQL extends the use of GROUP BY
so that the select list can refer to
nonaggregated columns not named in the GROUP BY
clause. This means
that the preceding query is legal in MySQL. You can use this feature
to get better performance by avoiding unnecessary column sorting and
grouping. However, this is useful primarily when all values in each
nonaggregated column not named in the GROUP BY
are the same for each
group. The server is free to choose any value from each group, so
unless they are the same, the values chosen are indeterminate.
Furthermore, the selection of values from each group cannot be
influenced by adding an ORDER BY clause. Sorting of the result set
occurs after values have been chosen, and ORDER BY does not affect
which values within each group the server chooses.
使用这个:
SELECT user_id, project_id, MAX(date_created) as latest
FROM influences
WHERE project_id = 1
GROUP BY user_id, project_id
工作原理:MySQL selects 所有符合 WHERE
条件的行,然后按 user_id
对它们进行排序,然后,对于每个 user_id
通过 project_id
。从每组具有相同 user_id
和 project_id
的行中,它将在最终结果集中生成一行。
您可以在 SELECT
子句中使用在 GROUP BY
子句中使用的列(user_id
和 project_id
);它们的值是明确的:每个组的所有行都具有相同的 user_id
和 project_id
.
您也可以使用 aggregate functions。它们中的每一个都使用组中所有行中的一列来计算单个值。最近的 created_at
当然是 MAX(created_at)
.
如果您 select 一个列既没有包含在 GROUP BY
子句中,也没有传递给聚合函数(比如您查询中的 created_at
),MySQL 没有提示如何计算该值。标准 SQL
禁止它(查询无效)但 MySQL allows it。它只会从该列中选择一个值,但无法让它从特定行中选择它,因为这实际上是未定义的行为。
您可以从 GROUP BY
子句中省略 project_id
,因为 WHERE
子句将使所有行具有相同的 project_id
。即使 project_id
没有出现在 GROUP BY
子句中并且它不是使用聚合函数计算的,这也会巧合地使结果正确。
我建议您将 project_id
保留在 GROUP BY
子句中。它不会影响结果或查询速度,它允许您放宽过滤条件(f.e。使用 WHERE project_id IN (1, 2)
)总是得到正确的结果(如果您将其从中删除,则不会发生这种情况) GROUP BY
).
我有以下内容:
user_id date_created project_id
3 10/10/2013 1
3 09/10/2013 1
5 10/10/2013 1
8 10/10/2013 1
10 10/10/2013 1
3 08/10/2013 1
我想要的最终结果是:
user_id date_created project_id
3 10/10/2013 1
5 10/10/2013 1
8 10/10/2013 1
10 10/10/2013 1
上下文:
我有一个叫做影响力的东西,一个用户可以对一个项目有很多影响力。 我想获取用户对项目的最新影响列表。
我试过:
select * from influences
where project_id = 1
group by user_id
ORDER BY created_at DESC
但是当然这忽略了首先由创建于 的用户排序,然后对完整列表进行排序。它只是简单地将用户挤压在一起并订购最终列表
LARAVEL - Eloquent 提供的答案是这样的:
return Influence::select( "user_id", "influence", DB::raw( "MAX(created_at) as created_at" ) )
->where( "project_id", "=", $projectID )
->groupBy( "user_id", "project_id" )->get();
您不想在 group by
之前订购,因为给定您的查询结构,它没有必要按照您的意愿进行。
如果您想要最近创建的影响力,请明确获取它:
select i.*
from influences i join
(select user_id, max(created_at) as maxca
from influences i
where project_id = 1
group by user_id
) iu
on iu.user_id = i.user_id and iu.maxca = i.created_at
where i.project_id = 1;
您的意图是使用文档 明确 警告不要使用的 MySQL 扩展。您想要包含 select
中不在 group by
中的列。正如 documentation 所说:
MySQL extends the use of
GROUP BY
so that the select list can refer to nonaggregated columns not named in theGROUP BY
clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in theGROUP BY
are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values within each group the server chooses.
使用这个:
SELECT user_id, project_id, MAX(date_created) as latest
FROM influences
WHERE project_id = 1
GROUP BY user_id, project_id
工作原理:MySQL selects 所有符合 WHERE
条件的行,然后按 user_id
对它们进行排序,然后,对于每个 user_id
通过 project_id
。从每组具有相同 user_id
和 project_id
的行中,它将在最终结果集中生成一行。
您可以在 SELECT
子句中使用在 GROUP BY
子句中使用的列(user_id
和 project_id
);它们的值是明确的:每个组的所有行都具有相同的 user_id
和 project_id
.
您也可以使用 aggregate functions。它们中的每一个都使用组中所有行中的一列来计算单个值。最近的 created_at
当然是 MAX(created_at)
.
如果您 select 一个列既没有包含在 GROUP BY
子句中,也没有传递给聚合函数(比如您查询中的 created_at
),MySQL 没有提示如何计算该值。标准 SQL
禁止它(查询无效)但 MySQL allows it。它只会从该列中选择一个值,但无法让它从特定行中选择它,因为这实际上是未定义的行为。
您可以从 GROUP BY
子句中省略 project_id
,因为 WHERE
子句将使所有行具有相同的 project_id
。即使 project_id
没有出现在 GROUP BY
子句中并且它不是使用聚合函数计算的,这也会巧合地使结果正确。
我建议您将 project_id
保留在 GROUP BY
子句中。它不会影响结果或查询速度,它允许您放宽过滤条件(f.e。使用 WHERE project_id IN (1, 2)
)总是得到正确的结果(如果您将其从中删除,则不会发生这种情况) GROUP BY
).