Sam van beastlo Sam van beastlo - 4 months ago 8
SQL Question

sql make 2 update queries 1

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.

Can you help me?

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


UPDATE:
Thanks for all the awnsers! i've accepted the first awnser

Answer

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;  
    

Hope it helps.

Comments