2015-12-21 6 views
0

Cont. на 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) 
Смежные вопросы