Subtract a field from the current time in MySQL?

I have the following:

Table Products:
With a column called “purchased_at” of type “datetime”.

Products.find(:all, :conditions => ["? - purchased_at >= 120",
Time.now])

Why doesn’t that work? What I’m trying to do is find records that were
purchased less than or equal to 2 minutes ago.

Thanks for your help.

Ben J. wrote:

I have the following:

Table Products:
With a column called “purchased_at” of type “datetime”.

Products.find(:all, :conditions => ["? - purchased_at >= 120",
Time.now])

Why doesn’t that work? What I’m trying to do is find records that were
purchased less than or equal to 2 minutes ago.

Thanks for your help.

I found the following to work:
Products.find(:all, :conditions => “NOW() - purchased_at >= 120”

Just udating it for later searches.

I found the following to work:
Products.find(:all, :conditions => “NOW() - purchased_at >= 120”

Just udating it for later searches.

SELECT something FROM tbl_name
WHERE DATE_SUB(CURDATE(),INTERVAL 2 MINUTES) <= date_col;

so your find should be like this:

Products.find(:all, :conditions => “DATE_SUB(CURDATE(),INTERVAL 2
MINUTES) >= purchased_at”)

Interval can be something like 2 MINUTES.
More info:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

regards,
rolando.-

On 7/25/06, Ben J. [email protected] wrote:

Thanks for your help.

What about this?

Products.find(:all, :conditions => ["(? - purchased_at) >= 120",
Time.now])

Does this work? Should result in a query like this:

SELECT * FROM products WHERE (the_current_datetime_here - purchased_at)

= 120

Hope that helps.

Hello Ben,

Table Products:
With a column called “purchased_at” of type “datetime”.

Products.find(:all, :conditions => ["? - purchased_at >= 120",
Time.now])

Why doesn’t that work? What I’m trying to do is find records that were
purchased less than or equal to 2 minutes ago.

Try sth like :

Product.find(:all, :conditions => [ “purchased_at > ?”,
2.minutes.ago.to_s(:db) ])

-- Jean-François.

Hi peepz,

I have quite a strange problem, when I visit my rails application in
internet explorer sometimes it won’t complete the page and just give
aerror. I saw that disabling external extensions fixes the problems but
it won’t. Firefox just workes fine.

Mongrel doesn’t give a stackstrace only invalid argument error that why
I listed the webrick error below…

It gives errors like:

127.0.0.1 - - [31/Jul/2006:23:47:42 W. Europe Standard Time] “GET
/images/edit.gif?1154286828 HTTP/1.1” 304 0
[2006-07-31 23:47:42] ERROR Errno::EINVAL: Invalid argument
C:/Program Files/ruby/lib/ruby/1.8/webrick/httpresponse.rb:324:in
write' C:/Program Files/ruby/lib/ruby/1.8/webrick/httpresponse.rb:324:in_write_data’
C:/Program Files/ruby/lib/ruby/1.8/webrick/httpresponse.rb:180:in
send_header' C:/Program Files/ruby/lib/ruby/1.8/webrick/httpresponse.rb:103:insend_response’
C:/Program Files/ruby/lib/ruby/1.8/webrick/httpserver.rb:79:in run' C:/Program Files/ruby/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
C:/Program Files/ruby/lib/ruby/1.8/webrick/server.rb:162:in
start_thread' C:/Program Files/ruby/lib/ruby/1.8/webrick/server.rb:95:instart’
C:/Program Files/ruby/lib/ruby/1.8/webrick/server.rb:92:in start' C:/Program Files/ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
C:/Program Files/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start’

./script/…/config/…/vendor/rails/railties/lib/webrick_server.rb:67:in
`dispatch’

./script/…/config/…/vendor/rails/railties/lib/commands/servers/webrick.rb:59
C:/Program
Files/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require’

./script/…/config/…/vendor/rails/activesupport/lib/active_support/dependencies.rb:147:in
require' ./script/../config/../vendor/rails/railties/lib/commands/server.rb:39 C:/Program Files/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
./script/server:3
-e:4

anyone know whats wrong?

Abdul

Strange, now I just get half generated pages in both internet explorer
and firefox…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs