[英]get max ids by group mysql
I have this table called sw_sowing
我有这张叫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. 我想知道id_unit的当前状态和id_variety(取决于日期)。 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
. 问题是id_unit = 3
有两个不同的id_variety
, state = 100
,但是前一个查询给出了最后一个,而我想要两个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! 我希望你能帮助我!
Just include id_variety on the GROUP BY. 只需在GROUP BY上包含id_variety。
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
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. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.