MySQL 根据组中(不在表中)另一个字段的最大值对一个字段进行分组

发布于 2024-10-21 18:38:54 字数 1439 浏览 2 评论 0原文

考虑下表:


un_id   avl_id  avl_date    avl_status
1738    6377398 2011-03-10  unavailable 
1738    6377399 2011-03-11  unavailable 
1738    6377400 2011-03-12  unavailable 
1738    6719067 2011-03-12  unavailable
1738    6719351 2011-03-12  available
1738    6377401 2011-03-13  unavailable 
1738    6377402 2011-03-14  unavailable 
1738    6377403 2011-03-15  unavailable 
1738    6377404 2011-03-16  available
1738    6719068 2011-03-16  unavailable 
1738    6719352 2011-03-16  available

这是从以下查询中获得的:


SELECT 
    tbl_unit.un_id,
    tbl_availability.avl_id,
    tbl_availability.avl_date, 
    tbl_availability.avl_status
FROM
    tbl_unit
INNER JOIN 
    tbl_availability ON 
    tbl_unit.un_id = tbl_availability.un_id
WHERE
    tbl_availability.avl_active='True' AND
    tbl_unit.un_active='True' AND
    tbl_availability.avl_date >= '2011-03-10' AND
    tbl_availability.avl_date 

我想要的是 GROUP BY un_id,以便仅显示具有最高 avl_id 的 avl_status。即:

un_id   avl_id  avl_date    avl_status
1738    6377398 2011-03-10  unavailable 
1738    6377399 2011-03-11  unavailable 
1738    6719351 2011-03-12  available
1738    6377401 2011-03-13  booked 
1738    6377402 2011-03-14  booked 
1738    6377403 2011-03-15  booked 
1738    6719352 2011-03-16  available

我尝试添加 GROUP BY 和 HAVING 子句以及各种子查询,但每次都失败......

感谢所有帮助! :) - 亚当。

Consider the following table:


un_id   avl_id  avl_date    avl_status
1738    6377398 2011-03-10  unavailable 
1738    6377399 2011-03-11  unavailable 
1738    6377400 2011-03-12  unavailable 
1738    6719067 2011-03-12  unavailable
1738    6719351 2011-03-12  available
1738    6377401 2011-03-13  unavailable 
1738    6377402 2011-03-14  unavailable 
1738    6377403 2011-03-15  unavailable 
1738    6377404 2011-03-16  available
1738    6719068 2011-03-16  unavailable 
1738    6719352 2011-03-16  available

Which is obtained from the following query:


SELECT 
    tbl_unit.un_id,
    tbl_availability.avl_id,
    tbl_availability.avl_date, 
    tbl_availability.avl_status
FROM
    tbl_unit
INNER JOIN 
    tbl_availability ON 
    tbl_unit.un_id = tbl_availability.un_id
WHERE
    tbl_availability.avl_active='True' AND
    tbl_unit.un_active='True' AND
    tbl_availability.avl_date >= '2011-03-10' AND
    tbl_availability.avl_date 

What I want is to GROUP BY un_id so that only the avl_status having the highest avl_id is displayed. i.e:

un_id   avl_id  avl_date    avl_status
1738    6377398 2011-03-10  unavailable 
1738    6377399 2011-03-11  unavailable 
1738    6719351 2011-03-12  available
1738    6377401 2011-03-13  booked 
1738    6377402 2011-03-14  booked 
1738    6377403 2011-03-15  booked 
1738    6719352 2011-03-16  available

I have tried adding GROUP BY and HAVING clauses and various subqueries, but I have failed every time....

All help appreciated! :)
- Adam.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

空‖城人不在 2024-10-28 18:38:54
SELECT 
    tbl_unit.un_id,
    tbl_availability.avl_id,
    tbl_availability.avl_date, 
    tbl_availability.avl_status
FROM
    tbl_unit
INNER JOIN 
    (select un_id, max(avl_id) as max_avl_id from tbl_availability group by un_id) T
    on tbl_unit.un_id = T.un_id
INNER JOIN tbl_availability ON 
    T.max_avl_id = tbl_availability.avl_id
WHERE
    tbl_availability.avl_active='True' AND
    tbl_unit.un_active='True' AND
    tbl_availability.avl_date >= '2011-03-10' AND
    tbl_availability.avl_date
SELECT 
    tbl_unit.un_id,
    tbl_availability.avl_id,
    tbl_availability.avl_date, 
    tbl_availability.avl_status
FROM
    tbl_unit
INNER JOIN 
    (select un_id, max(avl_id) as max_avl_id from tbl_availability group by un_id) T
    on tbl_unit.un_id = T.un_id
INNER JOIN tbl_availability ON 
    T.max_avl_id = tbl_availability.avl_id
WHERE
    tbl_availability.avl_active='True' AND
    tbl_unit.un_active='True' AND
    tbl_availability.avl_date >= '2011-03-10' AND
    tbl_availability.avl_date
蓝天白云 2024-10-28 18:38:54

试试这个:

SELECT
     un_id,avl_status
    FROM
    ( 
    SELECT 
        tbl_unit.un_id,
        tbl_availability.avl_id,
        tbl_availability.avl_date, 
        tbl_availability.avl_status
    FROM
        tbl_unit
    INNER JOIN 
        tbl_availability ON 
        tbl_unit.un_id = tbl_availability.un_id
    WHERE
        tbl_availability.avl_active='True' AND
        tbl_unit.un_active='True' AND
        tbl_availability.avl_date >= '2011-03-10' AND
        tbl_availability.avl_date 
    )
    GROUP BY 
     un_id,avl_status
    HAVING
     avl_id=max(avl_id) 

Try this:

SELECT
     un_id,avl_status
    FROM
    ( 
    SELECT 
        tbl_unit.un_id,
        tbl_availability.avl_id,
        tbl_availability.avl_date, 
        tbl_availability.avl_status
    FROM
        tbl_unit
    INNER JOIN 
        tbl_availability ON 
        tbl_unit.un_id = tbl_availability.un_id
    WHERE
        tbl_availability.avl_active='True' AND
        tbl_unit.un_active='True' AND
        tbl_availability.avl_date >= '2011-03-10' AND
        tbl_availability.avl_date 
    )
    GROUP BY 
     un_id,avl_status
    HAVING
     avl_id=max(avl_id) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文