1
我正在使用以下查詢來獲取用戶的最近記錄。加入與另一個表的SQL查詢
SELECT t1.username, t1.value, t1.date
FROM table t1
JOIN (select username, max(date) as maxdate from table
group by username) t2 on t1.username = t2.username
and t1.date = t2.maxdate
WHERE t1.date >= CURDATE()
ORDER BY t1.date DESC
它返回以下結構的表
------------------------
username | value | date
------------------------
我有以下結構
----------------------------
username | category | group
----------------------------
我怎樣才能得到類似下面的表格結構
結果的另一個表(T3)-------------------------------------------
username | value | date | category | group
-------------------------------------------
如何加入這些關係?
感謝您的解決方案。但不知何故,它的表現並不合格。見安德烈的答案。我已經嘗試了兩者。您的查詢在我的服務器中以24.03秒返回。相反,安德烈的查詢在2.46秒內返回。雖然謝謝 –
我同意,但這種解決方案不是面向性能的。 –