Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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 轨道计数选择不同_Mysql_Ruby On Rails_Count_Distinct - Fatal编程技术网

Mysql 轨道计数选择不同

Mysql 轨道计数选择不同,mysql,ruby-on-rails,count,distinct,Mysql,Ruby On Rails,Count,Distinct,我正在记录用户观看一系列视频的次数。现在我正试图绘制一张每天观看任何视频的用户数量图表 UserVideoWatching.where("created_at >= ? AND user_id != ?",1.month.ago, User.elephant.id).group("DATE(created_at)").reorder('created_at').count 生成sql SELECT COUNT(*) AS count_all, DATE(created_at) AS da

我正在记录用户观看一系列视频的次数。现在我正试图绘制一张每天观看任何视频的用户数量图表

UserVideoWatching.where("created_at >= ? AND user_id != ?",1.month.ago, User.elephant.id).group("DATE(created_at)").reorder('created_at').count
生成sql

SELECT COUNT(*) AS count_all, DATE(created_at) AS date_created_at FROM `user_video_watchings` WHERE (created_at >= '2013-01-27 10:43:24' AND user_id != 7) GROUP BY DATE(created_at) ORDER BY created_at
这将为每天观看的所有视频生成正确的结果,但正如我所说的,我只想向每个用户显示一次

我想要的sql是

SELECT COUNT(DISTINCT user_id) AS count_all, DATE(created_at) AS date_created FROM `user_video_watchings` WHERE (created_at >= '2013-01-27 10:33:18' AND user_id != 7) GROUP BY DATE(created_at) ORDER BY created_at
所以我想

UserVideoWatching.where("created_at >= ? AND user_id != ?",1.month.ago, User.elephant.id).group("DATE(created_at)").reorder('created_at').select('COUNT(DISTINCT user_id) AS count_all, DATE(created_at) AS date_created')
我会做我想做的事。但这给了

[#<UserVideoWatching >, #<UserVideoWatching >]
而不是一个杂烩

有什么想法吗

我使用的是rails 3.1和mysql,您可以使用distinct.count:attribute\u name

在Rails3中,使用:count:user\u id,而不是distinct:true

因此:

无法进行测试,但我认为这将产生您所追求的SQL。

在Rails 4中,使用…uniq.count:user\u id,如本问题的其他答案和其他地方所述,这实际上会导致查询中出现一个额外的独特存在:

从中选择DISTINCT COUNTDISTINCT user\u id

我们实际上要做的是自己使用SQL字符串:

..countDISTINCT用户标识

这给了我们:


选择COUNTDISTINCT user\u id FROM…

在rails 5.0.1中应使用distinct,distinct equal uniq,但

[11] pry(main)> Needremember.distinct.count(:word)
(1.1ms)  SELECT DISTINCT COUNT(DISTINCT "needremembers"."word") FROM "needremembers"
[12] pry(main)> Needremember.uniq.count(:word)
DEPRECATION WARNING: uniq is deprecated and will be removed from Rails 5.1 (use distinct instead) (called from __pry__ at (pry):12)
   (0.6ms)  SELECT DISTINCT COUNT(DISTINCT "needremembers"."word") FROM "needremembers"

rails 4中已弃用请参阅,了解使用ModelName.distinct.count:attribute\u name的解决方案如何返回模型的所有属性?
[11] pry(main)> Needremember.distinct.count(:word)
(1.1ms)  SELECT DISTINCT COUNT(DISTINCT "needremembers"."word") FROM "needremembers"
[12] pry(main)> Needremember.uniq.count(:word)
DEPRECATION WARNING: uniq is deprecated and will be removed from Rails 5.1 (use distinct instead) (called from __pry__ at (pry):12)
   (0.6ms)  SELECT DISTINCT COUNT(DISTINCT "needremembers"."word") FROM "needremembers"