3
考慮下面表中的最高值的字段:MySQL的組由基於另一場中的基團(未在表中)
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子句和各種子查詢,但我每次都失敗....
所有幫助感激! :) - 亞當。
看起來不錯,但也不能令人信服:#1054 - 未知列 'T.avl_id' 在 '關於條款' – Adam 2011-03-11 15:19:40
哎呀,我看到了問題,需要添加T.avl_id到子查詢SELECT子句... – Adam 2011-03-11 15:21:23
仍然無法正常工作,我也嘗試了INNER JOIN tbl_availability ON T.max_avl_id = tbl_availability.avl_id – Adam 2011-03-11 15:22:54