MySQL 通过 group by 从表中选择最小值

发布于 2024-10-15 08:14:08 字数 1562 浏览 6 评论 0原文

大家好
我有一个表,其中包含一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星光不落少年眉 2024-10-22 08:14:09

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

SELECT multiregistration, date 
FROM registrations 
ORDER BY multiregistration, date 

更新:

这将为您提供一组 ids...每个不同的多重注册,其中日期是最小(日期)...

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

最小日期是 1/1/2011 多重注册 (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

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.

SELECT multiregistration, date 
FROM registrations 
ORDER BY multiregistration, date 

UPDATE:

This will give you a set of ids...one for each distinct multiregistration where the date is the min(date)...

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

UPDATE:

This will give you a one multiregistration record...where the date is the first date in the table...

SELECT top 1 multiregistration  
FROM registrations     
ORDER BY date 

UPDATE:
(sample data?)

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) would be 1/1/2011 - id 2...
This will return an ordered (by ascending date) result set where the multiregistration value is 3

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

For both (actually in this case all 3 - #3, 2, and 4) in a list...the following would return a result set (shown below...)

SELECT * 
FROM registrations          
ORDER BY multiregistration, date

This returns a result set like

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
秋叶绚丽 2024-10-22 08:14:09

以下是我在 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

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.

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
时光是把杀猪刀 2024-10-22 08:14:09
SELECT MIN(date), `multiregistration` 
FROM `registrations` 
GROUP BY `multiregistration` 
ORDER by `date`
SELECT MIN(date), `multiregistration` 
FROM `registrations` 
GROUP BY `multiregistration` 
ORDER by `date`
來不及說愛妳 2024-10-22 08:14:09
SELECT multiregistration FROM registrations 
GROUP BY multiregistration ORDER BY date

它在做工作吗?

SELECT multiregistration FROM registrations 
GROUP BY multiregistration ORDER BY date

is it doing the work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文