vendredi 17 mars 2017

MySQL Update time with Random Date/Time except on a Variable Date

As you can see from my script below, I'm running an SQL query which takes three data fields and puts them into variables called oldtime, blackoutstart, and blackoutend.

With these variables, I want to update a different database table with a random date/time using the starting point as the oldtime variable. What I can't figure out is how to prevent that random date from falling within the blackoutstart<>blackoutend variables

    #!/bin/sh

    mysql DATABASE -B -N -s -e "select oldtime,blackoutstart,blackoutend from SOMETABLE;"| 
    while read -r line
    do
      oldtime=$(echo "$line" | cut -f1)
      blackoutstart=$(echo "$line" | cut -f2)
      blackoutend=$(echo "$line" | cut -f3)

    mysql DATABASE -e 'update TABLE set TIME="'"$oldtime"'" + interval floor(rand()*(60*60*24*7)) second;'

done

Now my brain says to just add something like:

where TIME NOT between "'"$blackoutstart"'" and "'"$blackoutend"'";

But I doubt that would fly, since it would reference the current dataset in TABLE.TIME and not the new data set I'm trying to apply.

So to the experts out there, what is a good way to randomly apply a date within the range of 1wk, while making sure it doesn't end up being one of the blackout dates between a range? Think of it like this, over the course of a year I would randomly assign a new date/time to the TIME field each week, but when holiday dates in the blackout variable (July 4 - July 5), I would want to make sure that my randomly assigned TIME field never gets a date/time between July 4th - July 5th.

Thanks,




Aucun commentaire:

Enregistrer un commentaire