目前我正在使用这个:
<div class="post-detail">
<?php
if($_SESSION["ses"]=="")
{
$cont="SELECT * FROM content WHERE id < ( SELECT MAX( id ) FROM content) ORDER BY id desc limit 1";
$cont_row=mysql_query($cont);
$cont_res=mysql_fetch_assoc($cont_row);
?>
<h3 class="post-title"><?php echo $cont_res["page_title"]?></h3>
<p class="post-description">
<?php
echo $cont_res["details"];
?>
</p>
<?php } ?>
<div class="read-more">
<a href="detail.html">Вижте повече...</a>
</div>
<div class="sign-up"></div>
</div>
但是我想显示的是表内容中id最高的数组。我已经尝试过使用max,但它不工作,给我一些错误:
<?php
if($_SESSION["ses"]=="")
{
$cont="select * from content where id='MAX(id)'";
//echo $cont;
//exit;
$cont_row=mysql_query($cont);
$cont_res=mysql_fetch_assoc($cont_row);
}
?>
这根本不起作用。有人可以显示在表内容中显示数组的最高id的正确方式?
最好和最简单的答案是对数据进行排序,然后限制返回值。@shubham在这个答案的第一行就做到了这一点,并且为了扩展它以覆盖您的评论,只需使用limit
的offset参数这样的:
// Returns the second highest ID (skip 1)
SELECT id, name, data FROM content ORDER BY id DESC LIMIT 1,1
// Return the top 3
SELECT id, name, data FROM content ORDER BY id DESC LIMIT 3
// Return sixth to eighth place.
SELECT id, name, data FROM content ORDER BY id DESC LIMIT 5, 3
编辑:以数据库为例:
mysql> select id,created FROM task ORDER by ID DESC limit 1;
+------+---------------------+
| id | created |
+------+---------------------+
| 7602 | 2016-09-23 12:28:04 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> select id,created FROM task ORDER by ID DESC limit 1,1;
+------+---------------------+
| id | created |
+------+---------------------+
| 7601 | 2016-09-23 10:01:36 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> select id,created FROM task ORDER by ID DESC limit 5,3;
+------+---------------------+
| id | created |
+------+---------------------+
| 7597 | 2016-09-22 12:56:49 |
| 7596 | 2016-09-22 12:41:44 |
| 7595 | 2016-09-22 11:22:02 |
+------+---------------------+
3 rows in set (0.00 sec)
那么,正如你所看到的,这是有效的
try this
SELECT * from content ORDER BY id DESC LIMIT 1;
如果您只想要一个这样的行,那么
SELECT row from content ORDER BY id DESC LIMIT 1
或
SELECT * FROM content WHERE id = (SELECT MAX(id) FROM content);
如果你想使用聚合函数,你也可以使用
进行过滤。select * from content having id=MAX(id);
对结果集进行工作…相反,在表上工作