Closed Thread
Results 1 to 4 of 4

Thread: Selecting Orders Before Some Date

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Selecting Orders Before Some Date

    Hi:

    What I'm trying to do is select all orders in my database before some date. I thought that I would use the < operator on the date in my WHERE clause but this isn't actually working.

    Code:
    SELECT SUM(amount) AS credit FROM group_trans WHERE group_id =5 AND trans_type = 'Credit' AND date < '2009-08-22'
    This query makes sense but for some odd reason it is returning the sum of all the orders even ones that are after 2009-08-22. Any thoughts? The field date is a Date field not a char field.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Selecting Orders Before Some Date

    I don't know what database you are using but that works in some (I think). I always convert the date to UNIXTIMESTAMP to make comparisons, addition and subtraction easy:

    [highlight=sql]SELECT SUM(amount) AS credit FROM group_trans WHERE group_id =5 AND trans_type = 'Credit' AND UNIX_TIMESTAMP(date) < UNIX_TIMESTAMP('2009-08-22')[/highlight]

    This works in MySQL. Not sure if other databases have this exact function but they probably have something similar at least.

    Source: MySQL :: MySQL 5.1 Reference Manual :: 11.6 Date and Time Functions

  4. #3
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Selecting Orders Before Some Date

    Thanks, I got it. D +rep!

  5. #4
    relapse's Avatar
    relapse is offline Programming Expert
    Join Date
    Jul 2009
    Location
    Intrawebs
    Posts
    479
    Blog Entries
    2
    Rep Power
    0

    Re: Selecting Orders Before Some Date

    What did you do to make it work?

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Amazon orders: Shipping?
    By zeroradius in forum The Lounge
    Replies: 3
    Last Post: 08-09-2010, 02:10 PM
  2. Selecting Via Combo Box
    By Bioshox in forum PHP Development
    Replies: 1
    Last Post: 02-18-2010, 01:49 AM
  3. Orders of growth in Scheme
    By infimum in forum General Programming
    Replies: 3
    Last Post: 11-17-2009, 11:07 AM
  4. Selecting something that is not there
    By Lokantis in forum Database & Database Programming
    Replies: 2
    Last Post: 01-31-2009, 04:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts