Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql查找最近的用户评论(第2部分)_Mysql - Fatal编程技术网

mysql查找最近的用户评论(第2部分)

mysql查找最近的用户评论(第2部分),mysql,Mysql,续 同样,有两个表: 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)

同样,有两个表:

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)
);

这次userCreateDate和commentID值与第1部分不同。
我想从数据库中查找最近的评论。

我的输出如下:

以下是我尝试的查询:

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
然而,我无法获得我的输出

有人能帮我吗?

您可以按以下方式执行此操作,名为
分组最大/最小值

请尝试以下查询:-

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)
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)