我有两个表
users : id,name
health : id,status,test_date,user_id
包含用户健康历史的健康表
现在我想获取特定用户的最后一次健康测试和用户信息
我试过这个查询
SELECT users.*, health.* FROM users INNER JOIN health ON users.id=health.user_id
having (max(health.id)) order by users.id desc limit 50
但是我失败了
最佳答案
试试这个:
SELECT users.*, health.* FROM users
INNER JOIN health
ON health.id = (SELECT id FROM health WHERE health.id = users.id ORDER BY id DESC LIMIT 1)
关于mysql - 获取一对多关系的最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29986098/