我有两张桌子:
Table 1
ID Username
1 human
2 humane
3 humaner
Table 2
ID userID date data1 data2
1 1 2017-10-25 25 12
2 1 2017-10-24 11 13
3 2 2017-10-25 43 24
我想只加入表2的最新数据:
username date data1 data2
human 2017-10-25 25 12
humane 2017-10-25 43 24
如何在php中使用mysql实现这一点? 我尝试用2个连接来做,但我认为它并不是真正的高效:)
答案 0 :(得分:0)
SELECT t1.*, t2.*
FROM Table1 t1
JOIN Table2 t2
ON t1.ID = t2.user_id
WHERE NOT EXISTS (SELECT 1
FROM Table2 Q
WHERE Q.user_id = t2.user_id
AND Q.date > t2.date)