MySql - 选择每行返回的总结果 [英] MySql - Selecting Total Results Returned In Each Row

查看:38
本文介绍了MySql - 选择每行返回的总结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算 SELECT 查询的每一行的结果总数.类似的东西:

I'm trying to count total number of results on each row of my SELECT query. Something like:

SELECT id, COUNT(*) FROM mytable;

但这仅返回带有计数的单行.如何获取每一行返回的总行数?

But this returns just a single row with the count. How do I get the total number of returned rows on every row?

我尝试使用:SELECT @i=@i+1(在子查询中)来实现这一点,但它不起作用.此外,尝试按 id 分组,但这也无济于事.

I've tried achieve this using: SELECT @i=@i+1 (in a sub query) but it doesn't work. Also, tried grouping by id but that doesn't help either.

是否有一些 MySql 函数返回返回的总行数,以便将其添加到结果的每一行中(用于计算)?

Is there some MySql function that returns total number of rows returned so that it could be added into each row of the result (for calculations)?

推荐答案

SELECT id, (SELECT COUNT(*) FROM mytable) FROM mytable;

也许你的意思是这样的?

Maybe you mean something like this?

这篇关于MySql - 选择每行返回的总结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆