Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7436355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:13:36+00:00 2026-05-29T10:13:36+00:00

I need to have a database with different rates at different time of day

  • 0

I need to have a database with different rates at different time of day and on different weekdays.

For example:
Between 10:00 and 16:00 monday to friday I have one rate. Between 16:00 and 10:00 monday to friday I have another. And on the weekends there is another rate.

The problem is when the time goes over midnight.

I have looked around for a solution. I have found this thread that I thought would work for me but it doesn’t. At least I can’t get it to work as I want it to.
Dealing with times and after midnight

The other solution I tried didn’t work either. It was to store time as TIME in mysql and check if the time and day was between the timespan in the database. This didn’t work out either because I didn’t find a good solution for when the timespan goes over midnight.

How should I try to solve this? What should the database look like and the query to get the data?

Edit:

Here is what it got so far. starttid = start time, sluttid = end time. Ovrig_tid is used when it’s not any of the specific times. startdag = start day of week, slutdag = end day of week. 0 = monday, 6 = sunday. So the result should be max one from every bolag_id.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:13:36+00:00Added an answer on May 29, 2026 at 10:13 am

    EDIT – Modified to meet criteria (end time) specified in comments:

    I believe what you’ll want to do is store each day’s rate separately. Store at least one value with the last minute of a given day as a final catch-all rate (this will be the only row for days with a single rate all day). At any given day/time, just consult this table to determine the given rate for that period of time. See below:

    DROP TABLE IF EXISTS tRate;
    CREATE TABLE tRate (
        rateId         INT(11) UNSIGNED NOT NULL auto_increment,
        rateDay        TINYINT(1),
        rateEndTime  TIME,
        rate           DECIMAL(9,2),
        PRIMARY KEY (rateId)
    )
    ;
    
    INSERT INTO tRate VALUES
    (NULL, 0, '00:10:00', '0.80'),
    (NULL, 0, '23:59:59', '0.90'),
    (NULL, 1, '00:10:00', '0.90'),
    (NULL, 1, '00:16:00', '0.75'),
    (NULL, 1, '23:59:59', '0.90')
    -- (etc. for all days 0-6)
    ;
    
    SET @execDay  = DATE_FORMAT(NOW(), '%w');       -- 1 in the case of today for the resultset below
    SET @execTime = DATE_FORMAT(NOW(), '%H:%m:%s'); -- 14:02:33 at the time this example was run
    

    Given this data, the following query:

    SELECT *
    FROM
        tRate
    WHERE
        rateDay = @execDay
        and @execTime < rateEndTime
    ;
    

    Returns the resultset for the particular execution time:

    +--------+---------+-------------+------+
    | rateId | rateDay | rateEndTime | rate |
    +--------+---------+-------------+------+
    |      5 |       1 | 23:59:59    | 0.90 |
    +--------+---------+-------------+------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need to sync auto_increment fields between two tables in different databases
I have database, and in one the tables I need change the values of
I have a database for which I need to aggregate records into another smaller
I have 2 data contexts in my application (different databases) and need to be
I have a database which I regularly need to import large amounts of data
I have a database entry that I need to update. This method obviously doesn't
I have a database table with a field that I need to read from
I have a database two tables and a linking table that I need a
I have done database optimization for dbs upto 3GB size. Need a really large
I have a database with customer information, orders, etc. I need to run a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.