3

I got two update queries and I want to make one out of them but I can't figure out how? I have looked through some other examples here on Stack Overflow but I just can't get it to work.

query 1:

UPDATE `exercise_list` 
SET  `ex1`= `ex2`, `ex2`= `ex3`,`ex4` = NULL 
WHERE `day_ID`= 1 AND `user_ID`= 4

query 2:

UPDATE `training_results` 
SET `ex1`= `ex2`, `ex2`= `ex3`,`ex4` = NULL  
WHERE `day_ID`= 1 AND `user_ID`= 4
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sam van beastlo
  • 819
  • 2
  • 11
  • 24
  • Possible duplicate of [MySQL, update multiple tables with one query](http://stackoverflow.com/questions/4361774/mysql-update-multiple-tables-with-one-query) – Jeremy Fortune Jul 30 '16 at 19:17

4 Answers4

1

There are two ways of achieving this:

  1. Using a single mySqlQuery call, simply call the mySqlQuery function in the following manner:

    mySqlQuery(UPDATE exercise_list SET ex1= ex2, ex2= ex3,ex4 = NULL WHERE  
    day_ID= 1 AND user_ID= 4;   
    UPDATE training_results SET ex1= ex2, ex2= ex3,ex4 = NULL WHERE  
    day_ID= 1 AND user_ID= 4;)  
    
  2. Method 2:

    UPDATE exercise_list e_l ,training_results t_r SET  
    e_l.ex1=e_l.ex2, e_l.ex2=e_l.ex3,e_l.ex4=NULL,  
    t_r.ex1=t_r.ex2, t_r.ex2=t_r.ex3,t_r.ex4=NULL  
    WHERE e_l.day_ID = 1 AND e_l.user_ID = 4 AND  
    t_r.day_ID = 1 AND t_r.user_ID = 4;  
    
Dharman
  • 30,962
  • 25
  • 85
  • 135
Milan Gupta
  • 1,181
  • 8
  • 21
0

Could be an inner join on the two tables

UPDATE exercise_list as a 
INNER JOIN training_results as b ON (a.day_ID = b.day_ID   AND a.user_ID = b.user_ID )
SET ex1= ex2, 
    ex2= ex3,
    ex4 = NULL
WHERE a.user_ID=4
AND a.day_ID = 1
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0
UPDATE A 
SET A.ex1 = A.ex2
, B.ex1 = B.ex2
, A.ex2 = A.ex3
, B.ex2 = B.ex3
, A.ex4 = NULL
, B.ex4 = NULL
FROM exercise_list A
JOIN training results B on  A.ID = B.ID
WHERE A.day_id = 1 and A.user_id = 4
Osie J O'Connor
  • 421
  • 7
  • 14
0

Probably this:

UPDATE exercise_list as el, training_results as tr SET  
el.ex1=el.ex2,el.ex2=el.ex3, el.ex4=NULL, tr.ex1=tr.ex2, tr.ex2=tr.ex3, tr.ex4=NULL  
WHERE el.day_ID = 1 AND el.user_ID = 4 AND tr.day_ID = 1 AND tr.user_ID = 4;
Milan Gupta
  • 1,181
  • 8
  • 21
A human being
  • 1,220
  • 8
  • 18