get max ids by group mysql

此生再无相见时 提交于 2019-12-25 04:14:34

问题


I have this table called sw_sowing

-----------------------------------------------------------------
 id  |   id_unit  |  date      |  id_variety |  type  |  status |
-----------------------------------------------------------------
  1  |      1     | 2017-08-10 |     1        |  SW   |    200  |
-----------------------------------------------------------------
  2  |      1     | 2017-10-10 |     1        |  ER   |    100  |
-----------------------------------------------------------------
  3  |      1     | 2017-11-30 |     2        |  SW   |    100  |
-----------------------------------------------------------------
  4  |      2     | 2017-12-10 |     3        |  SW   |    200  |
-----------------------------------------------------------------
  5  |      2     | 2017-12-10 |     3        |  ER   |    100  |
-----------------------------------------------------------------
  6  |      3     | 2017-08-05 |     4        |  SW   |    200  |
-----------------------------------------------------------------
  7  |      3     | 2017-12-13 |     4        |  ER   |    100  |
-----------------------------------------------------------------
  8  |      3     | 2018-01-04 |     1        |  SW   |    100  |
-----------------------------------------------------------------
  9  |      3     | 2018-01-04 |     2        |  SW   |    100  |
-----------------------------------------------------------------

I want to know the current status and id_variety of the id_unit depending on a date. I have this query:

SELECT sw_sowing.id_unit, sw_sowing.id_variety, sw_sowing.type, sw_sowing.status
FROM (
        SELECT MAX(id) AS id
        FROM sw_sowing
        WHERE date <= '2018-09-06'
        GROUP BY id_unit
) AS sw
INNER JOIN sw_sowing ON sw_sowing.id = sw.id

It gives me the following result:

----------------------------------------------------- 
   id_unit  |    id_variety |    type    |   status |
-----------------------------------------------------
      1     |        2      |     SW     |    100   |
-----------------------------------------------------
      2     |        3      |     ER     |    100   |
-----------------------------------------------------
      3     |        2      |     SW     |    100   |
-----------------------------------------------------

The problem is that the id_unit = 3 has two different id_variety with state = 100, but the previous query give the last one and I want the two id_variety.

Something like this:

----------------------------------------------------- 
   id_unit  |    id_variety |    type    |   status |
-----------------------------------------------------
      1     |        2      |     SW     |    100   |
-----------------------------------------------------
      2     |        3      |     ER     |    100   |
-----------------------------------------------------
      3     |        1      |     SW     |    100   |
-----------------------------------------------------
      3     |        2      |     SW     |    100   |
-----------------------------------------------------

I hope that you can help me!


回答1:


Just include id_variety on the GROUP BY.

    SELECT sw_sowing.id_unit, sw_sowing.id_variety, sw_sowing.type, sw_sowing.status
FROM (
        SELECT MAX(id) AS id
        FROM sw_sowing
        WHERE date <= '2018-09-06'
        GROUP BY id_unit,id_variety
) AS sw
INNER JOIN sw_sowing ON sw_sowing.id = sw.id



回答2:


I donot know why you have this requirement, but I think you can achieve this using the below query:

SELECT id_unit, id_variety, type, status FROM sw_sowing WHERE id IN (
  SELECT MAX(id) FROM sw_sowing WHERE (id_unit, date) IN (
    SELECT id_unit, MAX(date) FROM sw_sowing 
    WHERE date <= '2018-09-06'
    GROUP BY id_unit)
  GROUP BY id_unit, id_variety)

Hope this helps.



来源:https://stackoverflow.com/questions/52209300/get-max-ids-by-group-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!