Results 1 to 7 of 7

 

Thread: How can I select every date in a given month?

  1. #1
    Home is where I hang my @

    Status
    Offline
    Join Date
    Sep 2003
    Location
    My Bedroom
    Posts
    412
    Thanks
    0
    Thanked 0 Times in 0 Posts


    Hi,

    I need to select every date in a given month for a report. How can I do this in MySQL?

    Originally the report was selecting all data we have for certain days, but some days there maybe no data at all, so I want to display rows even though there is no data for that date and show it as a '0' or blank.

    So I thought the best way to do it would be to select all the days in a month first then see if there is any data on that date, but I don't know how to select every day in a given month.

    Any helpers?

    Thanks

    Daniel

  2. #2
    Businessman

    Status
    Offline
    Join Date
    Sep 2004
    Location
    West Yorkshire, UK
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What's your database look like?

  3. #3
    aka Antony

    Status
    Offline
    Join Date
    Mar 2005
    Location
    Bristol
    Posts
    531
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you using php?

    Basically you want


    SELECT field1,field2 FROM table WHERE datefield>='firstofmonth' AND datefield<='lastofmonth'

    firstofmonth and lastofmonth needed to be calculated in php/asp

  4. #4
    curiouser and curiouser

    Status
    Offline
    Join Date
    Sep 2003
    Location
    uk
    Posts
    659
    Thanks
    1
    Thanked 0 Times in 0 Posts
    appologies if this is wrong (im no expert at all)

    but if the dates are stored

    dd/mm/yy

    cant you display results based on wildcards for the dd and yy and using the month you need.


    again no expert and not sure on the exact syntax,

    im sure someone else will give you the full techy version.

    HTH

    Regards andy

    *edit* or you could do as above */edit*

  5. #5
    Home is where I hang my @

    Status
    Offline
    Join Date
    Sep 2003
    Location
    My Bedroom
    Posts
    412
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    OLBG - that would be fine, but I want to display a date even if there is no data corresponidng to that date in the DB.

    The way I have gone about it is to get the number of days in the month - in PHP this is:

    date("t")

    Then I have looped around every day in the month and within the loop fire a select to get the data (if any) from MySQL.

    If no data is returned, make the data/figure a '0', then display the date and '0' data, otherwise display the data and the database value.

    It works perfectly, and fast.

    But, the only downside I see is firing off 28-30 selects (1 for each day of the month) .

    I'd love to know if there is an easier way but with only using 1 SQL query.

    Daniel

  6. #6
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    323
    Thanks
    1
    Thanked 4 Times in 2 Posts
    Hi Daniel,

    It would make more sense to "fill in the blanks" in your application rather than make 31 separate queries. Logically, they achive the same result anyway.

    Use a query to select data for the whole month - along the lines of what OLBG suggests above; and then loop through the result creating a new array that either contains the data from the query for a particular day, or zero if no data exists. Then construct your report from the new array.

    If you can post the actual query that you are using at the moment we should be able to help out with some PHP code for the intermediate step...
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!

  7. #7
    Home is where I hang my @

    Status
    Offline
    Join Date
    Sep 2003
    Location
    My Bedroom
    Posts
    412
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks for the help. Your idea is great, I could have done it that way.

    The problem is now it's working, I don't want to change it, I'd rather get on with something else.

    If I find the report is being used a lot and gets slow then I'll probably change it, but I wonder how much faster an array would be compared to 30 fast selects.

    By the way I like the services you offer at FeedProcessor, I'd like to probably advertise or exchange links with you on a new project I am working on. So I may be in contact with you sometime.

    Thanks

    Daniel



Thread Information

Users Browsing this Thread

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

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
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2