Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql字段值之和-计数,带group by、order by、limit函数_Mysql - Fatal编程技术网

Mysql字段值之和-计数,带group by、order by、limit函数

Mysql字段值之和-计数,带group by、order by、limit函数,mysql,Mysql,我有表用户视图 我的平板电脑样本数据 Id | product_id | category | user_id | sitename| price | click_count | created_date 1 10 watch 102 ebay 820 1 2014-08-18 13:56:05 2 10 watch 102 amazon

我有表用户视图

我的平板电脑样本数据

Id | product_id | category | user_id |  sitename|   price | click_count | created_date
 1     10          watch     102          ebay       820     1            2014-08-18 13:56:05
 2     10          watch     102          amazon     750     1            2014-08-19 13:56:05
 3     10          watch     102          amazon     740     1            2014-08-19 18:00:05
 4     10          watch     102          ebay       940     1            2014-08-25 08:00:00
 5     10          watch     102          amazon     640     5            2014-08-25 08:10:10
 6     10          watch     102          ebay       580     3            2014-09-25 18:10:10
 7     10          watch     102          amazon     980     5            2014-10-05 12:20:40
我想要访问此产品的用户总数

我的问题

"select Id , proudct_id , category , user_id , count(click_count) as cnt from user_view where user_id =102 group by product_id order by rand() limit 0,10"
但输出仅显示一个计数

输出

 Id | product_id | category | user_id | cnt
  1     10          watch      102       1         
预期产量为

 Id | product_id | category | user_id |  cnt  
  1     10         watch       102       17  

您应该使用sum aggregate函数,而不是count函数,后者将计算行数。因此,您的查询应该类似于:

  select Id , product_id , category , user_id , SUM(click_count) as sum ...
试试这个:

select Id , proudct_id , category , user_id , SUM(click_count) as cnt
from user_view
 where user_id =102
order by rand()
limit 0,10
group by product_id

COUNT()
始终返回项数,而不是值的总和。因此,请使用
SUM()

重新阅读您的标题,然后再次查看您的代码。你问如何计算总数;但不使用求和函数。。。
"select Id , proudct_id , category , user_id , count(click_count) as cnt from user_view where user_id =102 group by user_id, product_id order by rand() limit 0,10"