[英]Select attribute of table based on min value from another table
My table structure is 我的表结构是
I want to get the recent video uploaded of the user 我想获取用户最近上传的视频
What I want to do is: 我想做的是:
"select video_id of a record who is having minimum of timestamp where userid='something'"
“选择时间戳记最小的记录的video_id,其中userid ='something'”
What I currently have is: 我目前拥有的是:
$recent_video = "$db->query("
SELECT video_id
FROM video_primary
ORDER BY timestamp DESC LIMIT 1
WHERE userid = '$userid'"
) or die($db->error);"`
while($row=mysqli_fetch_assoc($recent_video))
{
$video_id=$row['video_id'];
}
echo $video_id;
My table data is 我的表数据是
How do I get the most recent view uploaded by the user? 如何获取用户上载的最新视图?
Write WHERE
clause after FROM
part 在
FROM
部分之后编写WHERE
子句
Try this: 尝试这个:
SELECT vp.video_id
FROM video_primary vp
WHERE userid='$userid'
ORDER BY vp.timestamp DESC
LIMIT 1;
try this 尝试这个
$recent_video=$db->query("select video_id from video_primary where userid='$userid'
ORDER BY timestamp DESC LIMIT 1 ") or die($db->error);
instead of 代替
$recent_video="$db->query("select video_id from video_primary ORDER BY timestamp DESC
LIMIT 1 where userid='$userid'") or die($db->error);"
you are using where clause after order by and limit 1 which is a syntax error. 您在order by之后使用where子句,并且限制1,这是一个语法错误。 see tutorial
见教程
There is a specific sequence of every clause in Query statement. 查询语句中每个子句都有特定的顺序。 Below is the sequence:
以下是顺序:
So, as per the above sequence, you should correct your query like below. 因此,按照上述顺序,您应该像下面那样更正查询。 Also, you should place double quote(") correctly.
另外,您应该正确放置双引号(“)。
$recent_video = $db->query(" SELECT video_id FROM video_primary WHERE userid = '$userid' ORDER BY timestamp DESC LIMIT 1" ) or die($db->error);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.