Forum Moderators: open

Message Too Old, No Replies

UPDATE using a SELECT

creating an update query with select

         

howiek03

7:03 pm on Dec 15, 2008 (gmt 0)

10+ Year Member



hi all,

my first post and looking for help.

i am creating a database for practice in which i track my daily running time. i want to track and then determine how i have been improving on different timelines and platforms.

i am working with two tables running and runavg7. runavg7 stores the avg on a seven day basis by running platform and from the avg i have other calculations which will be performed later. i run on a treadmill, ellyptical, track and road and i am tracking each separately. i am tracking my run time for 7 days, 30 days, 60 days, 90 days. i also want to avg distance run... so if i can get runavg7 working then i can build the others.

sample data for past 7 days on treadmill:
runtime ¦ miles
----------------------
60 ¦ 4
60 ¦ 4
60 ¦ 4
60 ¦ 4
60 ¦ 4
60 ¦ 4
60 ¦ 4
---------

i created a select which works and gives me the correct avg:
SELECT AVG(runtime) FROM running WHERE platform = 'treadmill' and daynum <= 7;

AVG(runtime) ¦ AVG(miles)
------------
60 ¦ 4

ok thats to keep it simple. i normally run 40 - 60 mins a day. today i ran 35...

i would like to update runavg7 with the value produced by the SELECT above. i have an insert to create the record for runavg7 which works fine.

INSERT INTO runavg7 (platform, date) SELECT platform, date FROM running WHERE daynum=7;

the update i came up:
UPDATE runavg7 SET avg7days = (SELECT AVG(runtime) FROM running WHERE platform = 'treadmill' and daynum <= 7);
query ok, 0 records affected...
rows matched 0, ... 0, ... 0

i read an article about stored functions, but i dont know enough yet and am sure my trouble is easier than SF.

thanks for your help.

I am working with mysql v5+.

howiek03

7:01 am on Dec 16, 2008 (gmt 0)

10+ Year Member



ok, so i am clumsier than i am uninformed. my update was working but i hadnt run the insert so there was no record to which to apply the update. insert... update... all working...

LifeinAsia

4:40 pm on Dec 16, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Glad we could help. :)

Oh, and welcome [webmasterworld.com] to WW!