2015-12-21 37 views
0

續。在mysql find recent user commentsmysql查找最新用戶評論(第2部分)

而且,2個表:

create table user(
    userID int auto_increment, 
    userName varchar(10), 
    userCreatedDate timestamp, 
    primary key(userID) 
); 

create table comment(
commentID int auto-increment, 
userID int, 
comment varchar(100), 
primary key(commentID), 
foreign key(userID) references user(userID) 
); 

enter image description here

此番userCreateDate和commentID值是從第1部分
不同,我想找到從數據庫的最新評論。

我的輸出如下:

enter image description here

這裏是我試過的查詢:

select u.userID, max(c.commentID) as commentID, c.comment, u.userCreatedDate 
from comment c 
left join user u on c.userID = u.userID 
group by u.userID 
order by u.userCreateDate desc 

但是,我不能讓我的輸出。
有人可以幫助我嗎?

回答

0

嘗試此查詢: -

select u.userID,commentID, c.comment, u.userCreatedDate 
from comment c 
left join user u on c.userID = u.userID 
WHERE commentID=(SELECT MAX(c2.commentID) 
       FROM comment c2 
       WHERE c.userID = c2.userID) 
相關問題