【问题标题】:Show total amount of unique records in mySQL database显示 mySQL 数据库中唯一记录的总数
【发布时间】:2015-02-17 16:47:20
【问题描述】:

我正在尝试完成一项任务,其中我在 mySQL 数据库中有 100 万条记录,每条记录都有一个 URL 和一个引用 URL(当然还有一个 ID)。作业的部分目标是按“视图”显示前 5 个出现的 URL(AKA:每个 URL 在数据库中记录的频率)

我相信我非常接近,因为目前我显示了前 5 个 URL,但我的问题是我只能获得要显示的记录总数(1000000 或每个连续 URL 的视图彼此跟随在同一行)

我的控制器代码如下所示: (URL 是唯一出现在数据库中的)

@url = Url.where({ url: ["http://apple.com", "https://apple.com", "https://www.apple.com", "http://developer.apple.com", "http://en.wikipedia.org", "http://opensource.org"]}).group("url").limit(5).order(:created_at)
@viewcount = @url.count

我的视图代码如下所示:

<% @url.each do |url| %>
<%= url.url %> Views: <%= @viewcount %>
<% end %>

这是它目前的显示方式:

1st URL Views: {"1st URL"=>166420, "2nd URL"=>166182, "3rd URL"=>166804, "4th URL"=>167351, "5th URL"=>165933}

2nd URL Views: {"1st URL"=>166420, "2nd URL"=>166182, "3rd URL"=>166804, "4th URL"=>167351, "5th URL"=>165933}

3rd URL Views: {"1st URL"=>166420, "2nd URL"=>166182, "3rd URL"=>166804, "4th URL"=>167351, "5th URL"=>165933}

4th URL Views: {"1st URL"=>166420, "2nd URL"=>166182, "3rd URL"=>166804, "4th URL"=>167351, "5th URL"=>165933}

5th URL Views: {"1st URL"=>166420, "2nd URL"=>166182, "3rd URL"=>166804, "4th URL"=>167351, "5th URL"=>165933}

我假设问题出在控制器中的“where”语句上,我什至可能会不必要地使用第二行。

任何帮助将不胜感激。

编辑:

这是日志生成的代码:

Started GET "/top_urls" for ::1 at 2015-02-16 11:46:06 -0700
Processing by PagestatsController#top_urls as HTML
[1m[35m (4077.3ms)[0m  SELECT  COUNT(*) AS count_all, url AS url FROM `urls` WHERE `urls`.`url` IN ('http://apple.com', 'https://apple.com', 'https://www.apple.com', 'http://developer.apple.com', 'http://en.wikipedia.org', 'http://opensource.org') GROUP BY url  ORDER BY `urls`.`created_at` ASC LIMIT 5
[1m[36mUrl Load (4600.9ms)[0m  [1mSELECT  `urls`.* FROM `urls` WHERE `urls`.`url` IN ('http://apple.com', 'https://apple.com', 'https://www.apple.com', 'http://developer.apple.com', 'http://en.wikipedia.org', 'http://opensource.org') GROUP BY url  ORDER BY `urls`.`created_at` ASC LIMIT 5[0m
Rendered pagestats/top_urls.html.erb within layouts/application (4608.1ms)
Completed 200 OK in 8735ms (Views: 42.0ms | ActiveRecord: 8687.0ms)

【问题讨论】:

  • 正在生成什么查询?看看log/development.log 看看是不是你想要的。
  • 我得到:SELECT COUNT() AS count_all, url AS url FROM urls WHERE urls.url IN ('apple.com', 'apple.com', 'apple.com', 'developer.apple.com', 'en.wikipedia.org', 'opensource.org') GROUP BY url ORDER BY urls.created_at ASC LIMIT 5 [1m[36mUrl Load (4600.9ms)[0m [ 1mSELECT urls. FROM urls WHERE urls.url IN ('apple.com', 'apple.com', 'apple.com', 'developer.apple.com331', '@9876 @', 'opensource.org') GROUP BY url ORDER BY urls.created_at ASC LIMIT 5
  • 请注意,如果您只是将其附加到您的问题中,它看起来会少很多。奇怪的是,它应该返回一个单数,而不是每个项目的计数。
  • 对吗?!抱歉,我在这里是 n00b ..
  • 我收到“声明无效”错误:Mysql2::Error: Unknown column 'count_all' in 'order Clause': SELECT urls.* FROM urls GROUP BY url ORDER BY count_all LIMIT 5

标签: mysql ruby-on-rails ruby


【解决方案1】:

试试这个: @urls = Url.group('url').order('count(*) desc').limit(5).count

它会返回一个以url为key、view为value的哈希。

更新您的视图代码:

<% @urls.each do |url, view_count| %>
<%= url %> Views: <%= view_count %>
<% end %>

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 2018-03-18
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多