用sql / rails比较两个日期

时间:2018-07-12 15:55:20

标签: sql ruby-on-rails ruby

我对比较这些日期感到很困惑。我有一行代码是:

.where('invoices.invoice_status >= 100 AND invoices.shipping_mode = -1 AND invoices.archived_at IS NULL AND invoices.written_off_at IS NULL AND invoices.signup IS FALSE AND invoices.send_at >= ?', since_time)

send_at是Thu, 12 Jul 2018 17:18:53 CEST +02:00

因为时间是2018-07-12T17:12:37.673+02:00

我认为我的代码有错误,并且无法正常工作,因为这些日期时间看起来有所不同。我说得对吗?

更新

Hubspot::Setting Load (0.8ms)  SELECT  "hubspot_settings".* FROM "hubspot_settings" WHERE "hubspot_settings"."company_id" = $1 LIMIT 1  [["company_id", 10]]
19:09:04 log.1       |    (0.7ms)  SELECT COUNT(*) FROM "data_privacy_downloads" WHERE "data_privacy_downloads"."company_id" = $1  [["company_id", 10]]
19:09:04 log.1       |   User Load (0.5ms)  SELECT "users".* FROM "users" INNER JOIN "user_company_links" ON "users"."id" = "user_company_links"."user_id" WHERE "user_company_links"."company_id" = $1  [["company_id", 10]]
19:09:04 log.1       |    (0.6ms)  SELECT SUM("invoices"."actual_total") FROM "invoices" WHERE "invoices"."company_id" = $1 AND (invoice_status >= 100 AND archived_at IS NULL and is_duplicate IS NOT TRUE) AND ("invoices"."invoice_date" BETWEEN '2018-01-01' AND '2018-07-31') AND ("invoices"."invoice_date" BETWEEN '2018-02-01' AND '2018-02-28')  [["company_id", 10]]
19:09:04 log.1       |    (0.6ms)  SELECT SUM("invoices"."actual_total") FROM "invoices" WHERE "invoices"."company_id" = $1 AND (invoice_status >= 100 AND archived_at IS NULL and is_duplicate IS NOT TRUE) AND ("invoices"."invoice_date" BETWEEN '2018-01-01' AND '2018-07-31') AND ("invoices"."invoice_date" BETWEEN '2018-03-01' AND '2018-03-31')  [["company_id", 10]]
19:09:04 log.1       |    (0.6ms)  SELECT SUM("invoices"."actual_total") FROM "invoices" WHERE "invoices"."company_id" = $1 AND (invoice_status >= 100 AND archived_at IS NULL and is_duplicate IS NOT TRUE) AND ("invoices"."invoice_date" BETWEEN '2018-01-01' AND '2018-07-31') AND ("invoices"."invoice_date" BETWEEN '2018-04-01' AND '2018-04-30')  [["company_id", 10]]
19:09:04 log.1       |    (0.6ms)  SELECT SUM("invoices"."actual_total") FROM "invoices" WHERE "invoices"."company_id" = $1 AND (invoice_status >= 100 AND archived_at IS NULL and is_duplicate IS NOT TRUE) AND ("invoices"."invoice_date" BETWEEN '2018-01-01' AND '2018-07-31') AND ("invoices"."invoice_date" BETWEEN '2018-05-01' AND '2018-05-31')  [["company_id", 10]]
19:09:04 log.1       |    (1.8ms)  SELECT SUM("invoices"."actual_total") FROM "invoices" WHERE "invoices"."company_id" = $1 AND (invoice_status >= 100 AND archived_at IS NULL and is_duplicate IS NOT TRUE) AND ("invoices"."invoice_date" BETWEEN '2018-01-01' AND '2018-07-31') AND ("invoices"."invoice_date" BETWEEN '2018-06-01' AND '2018-06-30')  [["company_id", 10]]
19:09:04 log.1       |    (0.4ms)  SELECT SUM("invoices"."actual_total") FROM "invoices" WHERE "invoices"."company_id" = $1 AND (invoice_status >= 100 AND archived_at IS NULL and is_duplicate IS NOT TRUE) AND ("invoices"."invoice_date" BETWEEN '2018-01-01' AND '2018-07-31') AND ("invoices"."invoice_date" BETWEEN '2018-07-01' AND '2018-07-31')  [["company_id", 10]]
19:09:04 log.1       |   EmailSubscription Load (0.6ms)  SELECT "email_subscriptions".* FROM "email_subscriptions" WHERE "email_subscriptions"."company_id" = $1  ORDER BY email_subscriptions.email_type asc, lower(email_subscriptions.email) asc  [["company_id", 10]]
19:09:04 log.1       |    (0.7ms)  SELECT COUNT(*) FROM "invoices" WHERE "invoices"."company_id" = $1 AND (invoices.archived_at IS NULL AND invoices.signup IS FALSE AND invoices.send_at IS NOT NULL AND invoices.written_off_at IS NULL AND invoices.send_at >= DATE('2018-07-27') AND DATE(invoices.send_at) <= '2018-07-31 21:59:59.999999')  [["company_id", 10]]

1 个答案:

答案 0 :(得分:0)

我建议您解析日期,因为它们的格式不同:

.where('invoices.invoice_status >= 100 AND invoices.shipping_mode = -1 AND invoices.archived_at IS NULL AND invoices.written_off_at IS NULL AND invoices.signup IS FALSE AND DATE(invoices.send_at) >= ?', DATE(since_time))