Cowboy_BeBa
Registered User.
- Local time
- Tomorrow, 03:42
- Joined
- Nov 30, 2010
- Messages
- 188
hi all
im having a bit of trouble creating a query to help me keep my data up to date
basically im testing a page ive built that displays data within a certain date range, ive got queries to populate my tables with relevant test data but the problem is keeping this data up to date.
I wound up running the following query as a quick fix
I added 3 days because 3 days is the number of days since the test data was valid, so by adding it the test data becomes valid again.
This worked fine, but i needed to update the number of days manually, so i decided surely there has to be a way to automate it.
I figured if i used the date dif function to get the number of days between now() and the earliest date in tbl_batches it should keep my data up to date every time i run it (the earliest date in tbl_batches because that was the earliest date the data was valid when i created my test data, adjusting all the dates but that different replicates my test data and allows me to see if the page is working correctly)
so with that in mind i constructed this nested query
this query generates the exact number of days i need to add.
so far all this took me about 10 minutes to figure out
the next part, i thought, would be simple
delete the intger from the dateadd function and copy paste my query in its place. hence this
just to clear it up below is the same code, just spaced out a bit
now my problem is ive spent the past several hours rewriting this dozens of times and i cant figure out why this isnt working. tbh ive probably spent more time than i should have on this query (updating the values manually would have taken far less time), but at this point i just want to learn what im doing wrong so i wont make the same mistake in the future
can anyone see whats going wrong here?
im having a bit of trouble creating a query to help me keep my data up to date
basically im testing a page ive built that displays data within a certain date range, ive got queries to populate my tables with relevant test data but the problem is keeping this data up to date.
I wound up running the following query as a quick fix
Code:
UPDATE tbl_batches SET StartDate = DATE_ADD(StartDate, INTERVAL 3 day), EndDate = DATE_ADD(EndDate, INTERVAL 3 day);
I added 3 days because 3 days is the number of days since the test data was valid, so by adding it the test data becomes valid again.
This worked fine, but i needed to update the number of days manually, so i decided surely there has to be a way to automate it.
I figured if i used the date dif function to get the number of days between now() and the earliest date in tbl_batches it should keep my data up to date every time i run it (the earliest date in tbl_batches because that was the earliest date the data was valid when i created my test data, adjusting all the dates but that different replicates my test data and allows me to see if the page is working correctly)
so with that in mind i constructed this nested query
Code:
select DateDiff(now(), (select Min(StartDate) from tbl_batches)) as diff From tbl_batches group by diff;
this query generates the exact number of days i need to add.
so far all this took me about 10 minutes to figure out
the next part, i thought, would be simple
delete the intger from the dateadd function and copy paste my query in its place. hence this
Code:
UPDATE tbl_batches SET StartDate = DATE_ADD(StartDate, INTERVAL (select DateDiff(now(), (select Min(StartDate) from tbl_batches)) as diff From tbl_batches group by diff) day);
Code:
UPDATE tbl_batches SET StartDate =
DATE_ADD
(
StartDate,
INTERVAL
(
select DateDiff
(
now(),
(
select Min(StartDate) from tbl_batches
)
) as diff From tbl_batches group by diff
) day
)
now my problem is ive spent the past several hours rewriting this dozens of times and i cant figure out why this isnt working. tbh ive probably spent more time than i should have on this query (updating the values manually would have taken far less time), but at this point i just want to learn what im doing wrong so i wont make the same mistake in the future
can anyone see whats going wrong here?