mysql,取5条记录,保证第一条有图,有办法吗? - 技术问答

大家好求一个mysql语句,取5条记录,保证第一条有图。表名 item字段:id自增的整数picid有图就是一个整数,没图是0

create procedure select_content()begin??-- declare local variable to store the used record??declare used_id int;??-- select a record include image??and??use_id = id??select id from item where picid > 0 limit 0 , 1 into used_id;--union ??select * from item where id = used_id??union??select * from item where id used_id limit 0 , 4;??end;call select_content();
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-19
hey,guy.thanks for help,but there are something wrong[ol][*]mysql> create procedure select_content()[*]? ? -> begin[*]? ? ->? ?declare used_id int;[*]ERROR 1064 (42000): 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 \'\' at line 3[*]mysql>? ?select id from item where picid > 0 limit 0 , 1 into used_id;[*]ERROR 1327 (42000): Undeclared variable: used_id[*]mysql>? ?select * from item where id = used_id[*]? ? ->? ?union[*]? ? ->? ?select * from item where id used_id limit 0 , 4;[*]ERROR 1146 (42S02): Table \'supesite.item\' doesn\'t exist[*]mysql> end;[*]ERROR 1064 (42000): 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 \'end\' at line 1[*][/ol]
第2个回答  2013-08-19
SQL语句没有用吧??除非你加个字段 比如 ISIMAGE 或者的话取出记录 检索??取出
第3个回答  2013-08-19
自增的整数picid有图就是一个整数,没图是0
第4个回答  2020-09-26
对于楼主的问题没必要查询两遍。数据库里有
10000
条记录。你要查询
600-10000条时。完全可以
设置每页显示20条,然后如下:limit
600,
20查询一次,搞定。