怎样得到有limit的查询语句返回的记录数?

lxf_1976 2003-05-30 02:42:00
我使用select count(*) from tbl_name1 where username='white' limit 0, 20 返回的是所有username为white的记录数,请教如何得到带有limit的查询语句的返回记录数?(limit 0, 20的时候也可能只返回小于20条的记录)
...全文
65 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pwair 2003-05-30
  • 打赏
  • 举报
回复
select count(*) from (select * from tbl_name1 where username='white' limit 2,1 ) as a;
我用的是psql;是好用的。
table
Test=# select * from test1 ;
company_code | update_date | input_money
--------------+-------------+-------------
01 | 2003/03 | 10000
01 | 2003/04 | 20000
01 | 2003/05 | 30000
01 | 2003/06 | 40000
01 | 2003/07 | 20000
02 | 2003/02 | 20000
02 | 2003/03 | 20000
02 | 2003/04 | 20000
02 | 2003/05 | 30000
(9 rows)

Test=# select * from (select * from test1 where company_code='02' limit 2,1 ) as a;
company_code | update_date | input_money
--------------+-------------+-------------
02 | 2003/03 | 20000
02 | 2003/04 | 20000
(2 rows)
Test=# select count(*) from (select * from test1 where company_code='01' limit 2,4) as a;
count
-------
1
(1 row)
Test=# select * from (select * from test1 where company_code='01' limit 2,4 ) as a;
company_code | update_date | input_money
--------------+-------------+-------------
01 | 2003/07 | 20000
(1 row)
Test=# select count(*) from (select * from test1 where company_code='01' limit 2,5 ) as a;
count
-------
0
(1 row)

Test=# select * from (select * from test1 where company_code='01' limit 2,5 ) as a;
company_code | update_date | input_money
--------------+-------------+-------------
(0 rows)
lxf_1976 2003-05-30
  • 打赏
  • 举报
回复
Arbow的方法对

我的mysql版本是3.23.44,运行select count(*) from tbl_name1 where username='white' limit 0, 20总是返回所有满足条件的记录数
Arbow 2003-05-30
  • 打赏
  • 举报
回复
子查询吗?
我的3.23.56运行出错。
pwair 2003-05-30
  • 打赏
  • 举报
回复
select count(*) from (select * from tbl_name1 limit 0,20) as a;
Arbow 2003-05-30
  • 打赏
  • 举报
回复
我看只能这样:
select if ( count(*)>20, 20, count(*) ) as Number from tbl_name1 where username='white' limit 0, 20
反正当所有符合条件的记录数大于20的时候,查询出来的结构就是20条,用这个应该可以吧。
lumberjake 2003-05-30
  • 打赏
  • 举报
回复
1、小与20条,那就是count(*)返回的数目
2、大于20条,就是你limit限制的数!

为什么还要获得呢?

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧