MySQL 通过 group by 从表中选择最小值
大家好
我有一个表,其中包含一个名为 id 的自动增量值、一个用户 ID、一个日期(称为 date)和一个用于多个注册的整数(称为多重注册)
由于多重注册可以有多个日期,我需要首先选择日期最低的多重注册
您可以通过获取每个多重注册的最短日期来进行比较,其中我需要获取每个多重注册的(id)。然后它应该按最小日期对它们进行排序
一些示例数据
id --- user-id --- date --- multiregistration
1 | 2 | 2010-02-01 | 1
2 | 3 | 2010-02-01 | 2
3 | 4 | 2010-01-01 | 2
4 | 2 | 2010-02-03 | 1
5 | 4 | 2010-02-03 | 3
6 | 1 | 2010-02-02 | 3
预期输出
multiregistration
2
1
3
您可以将其与
SELECT `multiregistration`
FROM `registrations`
ORDER BY `date`
但我想删除重复项
进行比较这是我现在正在使用的PHP代码
// Create the query
$query = 'SELECT `multiregistration` AS `multireg` FROM `registrations` WHERE `multiregistration`>0 GROUP BY `multiregistration`';
// Execute the query and set the result into a variable
$idsTo2ndQuery = arrayExecuteLocalQueryGetResults($query);
// Create the second Query
$query2 = 'SELECT `multiregistration` AS `multireg` FROM `registrations` WHERE `multiregistration`='.implode(' OR `multiregistration`=', $idsTo2ndQuery).' ORDER BY `date` ASC';
// An array of IDs (sorted)
$ids = array();
// Execute the query and set the result into a variable
$idsArray = arrayExecuteLocalQueryGetResults($query2);
// For each entry
foreach ($idsArray AS $idArray)
if (!in_array($idArray, $ids))
$ids[] = $idArray;
// Return the IDs
return $ids;
Hey everyone
I have a table with an auto increment value called id, an user-id, a date (called date) and an integer for multiple registrations (called multiregistration)
As multiregistration can have more than one date I need to select the multiregistrations with the lowest date first
You can compare it by getting the minimum date for each multiregistration, where I need to get the (id for) each multiregistration. It should then sort them by the minimum date
Some sample data
id --- user-id --- date --- multiregistration
1 | 2 | 2010-02-01 | 1
2 | 3 | 2010-02-01 | 2
3 | 4 | 2010-01-01 | 2
4 | 2 | 2010-02-03 | 1
5 | 4 | 2010-02-03 | 3
6 | 1 | 2010-02-02 | 3
Expected output
multiregistration
2
1
3
You can compare it to
SELECT `multiregistration`
FROM `registrations`
ORDER BY `date`
But I want to remove duplicates
Here is the PHP code I am using right now
// Create the query
$query = 'SELECT `multiregistration` AS `multireg` FROM `registrations` WHERE `multiregistration`>0 GROUP BY `multiregistration`';
// Execute the query and set the result into a variable
$idsTo2ndQuery = arrayExecuteLocalQueryGetResults($query);
// Create the second Query
$query2 = 'SELECT `multiregistration` AS `multireg` FROM `registrations` WHERE `multiregistration`='.implode(' OR `multiregistration`=', $idsTo2ndQuery).' ORDER BY `date` ASC';
// An array of IDs (sorted)
$ids = array();
// Execute the query and set the result into a variable
$idsArray = arrayExecuteLocalQueryGetResults($query2);
// For each entry
foreach ($idsArray AS $idArray)
if (!in_array($idArray, $ids))
$ids[] = $idArray;
// Return the IDs
return $ids;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果按多重注册进行分组,则每个不同的多重注册只能获得一行。这将为您提供首先按多重注册排序,然后按日期排序的所有行的集合。
更新:
这将为您提供一组 ids...每个不同的多重注册,其中日期是最小(日期)...
更新:
这将为您提供一个多重注册记录...其中日期是第一个日期在表中...
更新:
(示例数据?)
最小日期是 1/1/2011 多重注册 (3) 将为 1/1/2011 - id 2...
这将返回一个有序(按升序日期)结果集,其中多重注册值为 3
对于列表中的两者(实际上在本例中为所有 3 - #3、2 和 4)...以下将返回一个结果集 (如下所示...)
这将返回一个结果集,例如
If you group by multiregistration, you'll only get one row per distinct multiregistration. This will give you the set of all the rows sorted by multiregistration first and then date.
UPDATE:
This will give you a set of ids...one for each distinct multiregistration where the date is the min(date)...
UPDATE:
This will give you a one multiregistration record...where the date is the first date in the table...
UPDATE:
(sample data?)
Minumum date is 1/1/2011 multiregistration (3) would be 1/1/2011 - id 2...
This will return an ordered (by ascending date) result set where the multiregistration value is 3
For both (actually in this case all 3 - #3, 2, and 4) in a list...the following would return a result set (shown below...)
This returns a result set like
以下是我在 T-SQL 中执行此操作的方法。它声明一个变量表来处理数据,并且查询会生成所请求的确切结果。
Here is how I would do this in T-SQL. It declares a variable table to play with the data and the query produces the exact result requested.
它在做工作吗?
is it doing the work?