2014-12-04 27 views
2

我在SQL這種類型的數據庫表的有關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' 

刪除同一用戶ID行只能選擇(使用SQLI PHP)

回答

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; 
+0

我可以使用SELECT * – 2014-12-04 06:12:35