in SQL/PHP returning id with the highest and 2nd-5th highest date
As said in the title, I need FIVE queries that returns the ID for rows with the 1st-5th most recent date.
如标题中所述,我需要五个查询返回具有第1~5个最近日期的行的ID。
Table: film
id releasedate
232143 2013-06-20
536523 2013-07-20
453554 2013-08-20
098776 2013-09-20
549302 2013-10-20
i.e the first query would return the id 549302
即第一个查询将返回id 549302
I think this would work for the first query:
我认为这适用于第一个查询:
$first = $db->query("SELECT id, FROM film WHERE MAX(releasedate)" );
PS: Sorry for the poor formatting of this post, can anyone tell me how to display tables appropriately?
PS:很抱歉这篇文章格式不佳,谁能告诉我如何正确显示表格?
I need to display each id at different points on the web page. Simply returning a list of ids won't suffice. What I really need is for each id to be encapsulated into a unique variable so i can call them at different points on the web page.
我需要在网页的不同位置显示每个ID。简单地返回一个id列表是不够的。我真正需要的是将每个id封装到一个唯一的变量中,这样我就可以在网页的不同位置调用它们。
2 个解决方案
#1
1
No, you don't need five queries.
不,您不需要五个查询。
$first = $db->query("SELECT `id` FROM `film` ORDER BY `releasedate` DESC LIMIT 5" );
This will get the IDs from the database of the five most recent films in your table.
这将从您桌上五部最新电影的数据库中获取ID。
To access each of these just run through a while
loop.
要访问其中的每一个,只需运行一个while循环。
while($row = $first->fetch_assoc()) {
$row['id']; # Each ID will be available like this.
}
更多相关文章
- ubuntu_mysql怎么判断自己的库和头文件的位置
- centos7 移动mysql5.7.19 数据存储位置
- 推荐几款制作网页滚动动画的 JavaScript 库
- [转]在网页中加入声音文件,并且用JavaScript对其进行播放控制
- js金额数字格式化实现代码(三位加逗号处理保留两位置小数)
- js“DOM事件”之鼠标事件、js的测试方法、js代码的放置位置
- 使用谷歌地图computeDistanceBetween获取最近的位置返回NaN
- Google Maps API v3:如何设置缩放级别和地图中心到用户提交的位置
- 拒绝获取不安全标题“位置”