MySQL从具有group by的表中选择最小值

时间:2011-02-02 13:26:41

标签: sql mysql select group-by

嘿大家喔 我有一个表,其中包含一个名为id的自动增量值,一个用户ID,一个日期(称为日期)和一个用于多个注册的整数(称为多重注册)
由于多注册可以有多个日期,我需要选择具有最低日期的多注册 您可以通过获取每个多注册的最小日期来比较它,我需要获取每个多注册的(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;

4 个答案:

答案 0 :(得分:1)

如果按多重注册进行分组,则每个不同的多重注册只能获得一行。这将为您提供首先按多重注册排序的所有行的集合,然后是日期。

SELECT multiregistration, date 
FROM registrations 
ORDER BY multiregistration, date 

更新:

这将为您提供一组ID ...每个不同的多重注册,其中日期为分钟(日期)...

SELECT id  
FROM registrations 
GROUP BY multiregistration
ORDER BY multiregistration, date 

更新:

这将为您提供一个多注册记录...其中日期是表格中的第一个日期......

SELECT top 1 multiregistration  
FROM registrations     
ORDER BY date 

更新: (样本数据?)

ID-----USER-ID-----DATE------------MULTIREGISTRATION
1        101            1/2/2011         3
2        101            1/1/2011         3 
3        102            1/5/2011         2
4        102            1/7/2011         2
5        103            1/1/2011         4

Minumum date is 1/1/2011 multiregistration(3)将是1/1/2011 - id 2 ... 这将返回一个有序(按升序日期)结果集,其中多重注册值为3

SELECT * 
FROM registrations   
WHERE ID in (SELECT min(date) FROM registrations)   
ORDER BY date 

对于列表中的两者(实际上在这种情况下都是3 - #3,2和4)...以下将返回结果集(如下所示......)

SELECT * 
FROM registrations          
ORDER BY multiregistration, date

返回结果集,如

3        102            1/5/2011         2
4        102            1/7/2011         2
2        101            1/1/2011         3 
1        101            1/2/2011         3
5        103            1/1/2011         4

答案 1 :(得分:1)

以下是我将如何在T-SQL中执行此操作。它声明了一个与数据一起使用的变量表,并且查询产生了所请求的确切结果。

DECLARE @A AS TABLE(ID INT, USERID INT, SOMEDATE DATE, MULTIREG INT)

INSERT @A
SELECT 1,2,'20100201',1 UNION ALL
SELECT 2,3,'20100201',2 UNION ALL
SELECT 3,4,'20100101',2 UNION ALL
SELECT 4,2,'20100302',1 UNION ALL
SELECT 5,4,'20100203',3 UNION ALL
SELECT 6,1,'20100202',3

SELECT * FROM @A AS A
WHERE A.SOMEDATE = (SELECT MIN(SOMEDATE) FROM @A AS B WHERE B.MULTIREG = A.MULTIREG)
ORDER BY USERID DESC

ID  USERID  SOMEDATE     MULTIREG
3    4     2010-01-01       2
1    2     2010-02-01       1
6    1     2010-02-02       3

答案 2 :(得分:0)

SELECT MIN(date), `multiregistration` 
FROM `registrations` 
GROUP BY `multiregistration` 
ORDER by `date`

答案 3 :(得分:-1)

SELECT multiregistration FROM registrations 
GROUP BY multiregistration ORDER BY date

它正在做这项工作吗?