gpt4 book ai didi

MySQL 查询在 Rails 3.2.11 的 postgresql 中不起作用

转载 作者:行者123 更新时间:2023-11-29 14:02:42 26 4
gpt4 key购买 nike

我在 Rails 中使用以下查询

grouped_link_counters = uattachment).link_counters.group("year(created_at), month(created_at)").count("created_at")

它使用 mysql 适配器在 Rails 控制台中给出以下结果。

  UserAttachment Load (0.2ms)  SELECT `user_attachments`.* FROM `user_attachments` WHERE `user_attachments`.`id` = 132 LIMIT 1
(0.2ms) SELECT COUNT(`link_counters`.`created_at`) AS count_created_at, year(created_at), month(created_at) AS year_created_at_month_created_at FROM `link_counters` WHERE `link_counters`.`user_attachment_id` = 132 GROUP BY year(created_at), month(created_at)
=> {11=>9, 12=>15, 1=>1, 2=>1}

但是当我使用 postgresql 时,相同的命令会抛出以下错误:-

SELECT COUNT("link_counters"."created_at") AS count_created_at, year(created_at), month(created_at) AS year_created_at_month_created_at FROM "link_counters" WHERE "link_counters"."user_attachment_id" = 1 GROUP BY year(created_at), month(created_at)
ActiveRecord::StatementInvalid: PG::Error: ERROR: function year(timestamp without time zone) does not exist
LINE 1: ...link_counters"."created_at") AS count_created_at, year(creat...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
: SELECT COUNT("link_counters"."created_at") AS count_created_at, year(created_at), month(created_at) AS year_created_at_month_created_at FROM "link_counters" WHERE "link_counters"."user_attachment_id" = 1 GROUP BY year(created_at), month(created_at)
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in `async_exec'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in `exec_no_cache'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:662:in `block in exec_query'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activesupport-3.2.11/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `exec_query'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1248:in `select'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:289:in `execute_grouped_calculation'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:206:in `perform_calculation'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:159:in `calculate'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:58:in `count'
from (irb):63
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3p194 :064 >

任何人都可以告诉我如何才能从 postgresql 获得相同的响应。提前致谢!

最佳答案

在 PostgreSQL 中,我们没有像 YEARMONTH 等辅助函数。您可以在 PostgreSQL 中使用 date_part 来代替这些函数与 MySQL 的结果相同。

例如

grouped_link_counters = uattachment.link_counters.group("date_part('year', created_at), date_part('month', created_at)").count("created_at")

关于MySQL 查询在 Rails 3.2.11 的 postgresql 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14833438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com