我有一个包含2组用户和记录的数据库。所以我想列出某个组中每个用户的最新记录,但结果显示的是我刚刚用户的最新和第一条记录更新记录为'27/4/300'和'4/5/2108'。“tarikh”的日期属性在Record表中,而users_id在Users表中。有人知道更好的查询以获得结果我想要吗?
$sql = "SELECT * from Users, Record
WHERE Users.users_id = Record.users_id
AND Users.group_id='$users_group'
AND Record.tarikh IN (SELECT max(Record.tarikh) FROM Record
GROUP BY users_id)";
我正在使用sql server management studio 17
答案 0 :(得分:1)
试试这个。
$sql = "SELECT * from Users, Record
WHERE Users.users_id = Record.users_id
AND Users.group_id='$users_group'
AND Record.tarikh = (SELECT max(Record_nested.tarikh) FROM Record as Record_nested
where Record_nested.users_id = Record.users_id)";