我在sql中有这种类型的数据库表
id | state | msg | userid | time |
1 |unread | hi | 1 | time |
2 |unread |hello| 1 | time |
3 |unread | hi | 2 | time |
4 | read | hi | 2 | time |
我已经开始打印行
Where state=unread
现在输出
= userid 1 send you 'hi'
= userid 1 send you 'hello'
= userid 2 send you 'hi'
但我想以这种方式输出
= userid 1 send you 'hello'
= userid 2 send you 'hi'
删除相同的用户标识行并仅选择最后一行(使用带sqli的php)
答案 0 :(得分:0)
试试这个:
SELECT a.id, a.state, a.msg, a.userid, a.time
FROM tableA a
INNER JOIN (SELECT a.userid, MAX(a.id) id
FROM tableA a
WHERE a.state = 'unread'
GROUP BY a.userid
) AS b ON a.userid = b.userid AND a.id = b.id;