findAll 返回前5条记录应该怎么写?
发布于:2022-01-17 09:50:54
#1 jiayan41
findAll(array("type" => 'product'),"sort DESC");
findAll 返回前5条记录应该怎么写?
findAll(array("type" => 'post'),"id DESC","LIMIT 0, 5");
这样写不行啊,呵呵
2010-05-10 16:28:44
#2 ryk007
findAll(array("type" => 'product'),"sort DESC");
findAll 返回前5条记录应该怎么写?
findAll(array("type" => 'post'),"id DESC","LIMIT 0, 5");
你这个样子写下嘛,,虽然不标准不过可以用
findAll(array("type" => 'post'),"id desc limit 5");
2010-05-10 16:31:36
#3 jiayan41
findAll(array("type" => 'post'),"id desc limit 5"); 这样写是不行的,一个都不显示
2010-05-10 16:35:12
#4 redguan
findAll($conditions = null, $sort = null, $fields = null, $limit = null)
这个是完整格式。中间参数没有,填 null
新(对):
findAll(array("type" => 'post'),"id desc",null,"3, 5");
旧(错):
findAll(array("type" => 'post'),null, "id desc","3, 5");
更复杂:
findAll(array("type" => 'post'), "id desc, `time` desc, `uid` asc ",'`id`,`uid`,`time`', "3, 5");
2010-05-10 16:38:31
#5 jiayan41
这个是完整格式。中间参数没有,填 null
更复杂:
redguan 发表于 2010-5-10 16:38
还是不行
SELECT j_contents.id desc FROM j_contents WHERE type = 'post' ORDER BY id LIMIT 3, 5
执行错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc FROM j_contents WHERE type = 'post' ORDER BY id LIMIT 3, 5' at line 1
2010-05-10 16:44:32
#6 jake
嗯,正确的做法是
http://speedphp.com/manual/spmodelfindAllarray findAll(mixed conditions, string sort, string fields, string limit)
findAll($conditions, null, null, '3,5');
2010-05-10 17:21:34
#7 jiayan41
问题找到了,如果 SQL语句获取post的前十条没有相关分类ID的文章,就不会显示了。
问题需要在smarty输出的时候,让smarty循环输出5条记录就好了
我正在找smarty显示5条的方法,谁有,给我最好,哈哈
2010-05-10 17:36:26
#8 改变命运2023
那如果是从第二个开始,到最后呢
2013-02-19 15:10:36
#9 jake
改变命运2023 发表于 2013-2-19 15:10
那如果是从第二个开始,到最后呢
limit参数控制
2013-02-19 15:37:49
#10 改变命运2023
jake 发表于 2013-2-19 15:37
limit参数控制
是这么个情况,因为是从第二条到最后一条的记录,而最后并不清楚具体是几条。当然也可以用count函数查出总共有几条,但是有没有其它更简单的办法,我之后又自己了一个sql语句"SELECT * FROM tj_pic WHERE pic_id not in(select top 1 pic_id from tj_pic ORDER BY pic_id)"报语法错误。无论是使用像您说的配置limit参数还是后面的sql语句,只要能解决问题就行,谢谢
2013-02-19 16:38:48
#11 改变命运2023
改变命运2023 发表于 2013-2-19 16:38
是这么个情况,因为是从第二条到最后一条的记录,而最后并不清楚具体是几条。当然也可以用count函数查出 ...
后来查到,mysql没有top关键字,因为以前是学mssql的,所以出现了这种错误,然后写成“ select * from tj_pic where pic_id not in(select pic_id from tj_pic limit 0,1 ) ORDER BY pic_id”。结果报的
执行错误: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'。说版本不支持,我的apache 是集成的,版本为5.2.0。难道我必须换apache版本吗?
2013-02-19 17:25:18
#12 jake
改变命运2023 发表于 2013-2-19 17:25
后来查到,mysql没有top关键字,因为以前是学mssql的,所以出现了这种错误,然后写成“ select * from t ...
就算有top关键字也查不到,top的作用和limit差不多。没见过你这种需求,count一下总数吧
2013-02-19 21:38:38
#13 改变命运2023
jake 发表于 2013-2-19 21:38
就算有top关键字也查不到,top的作用和limit差不多。没见过你这种需求,count一下总数吧 ...
呵呵,其实这个需求也是被逼无奈,那就如你所说,count一下总数吧。谢谢了
2013-02-19 21:44:42