1

I have the following update statement

UPDATE mytable
    set tmax = (
                SELECT tmax
                FROM myvalues
                WHERE Longitude_MAX >= v_lng and 
                      STR_TO_DATE(Date, '%m/%d/%Y') = v_date
                      limit 1),
        tmin = (
                SELECT tmin
                FROM myvalues
                WHERE Longitude_MAX >= v_lng and 
                      STR_TO_DATE(Date, '%m/%d/%Y') = v_date
                      limit 1),    
    where pkid = v_pkid;

its working but very slow, can we optimize it?

there are some example on net, but these join the both tables and have no such join.

2
  • what JOIN? Do I need to join both tables? Commented Mar 22, 2014 at 9:25
  • You need to join one table to the other table Commented Mar 22, 2014 at 9:27

1 Answer 1

2

Try that with JOIN

   UPDATE mytable  mt
   INNER JOIN myvalues mv
   ON STR_TO_DATE(mv.Date, '%m/%d/%Y') = mt.accident_dt
   SET mt.tmax = mv.tmax , mt.tmin=mv.tmin
   WHERE Longitude_MAX >= v_lng 
   and STR_TO_DATE(Date, '%m/%d/%Y') = v_date
   AND pkid = v_pkid;
Sign up to request clarification or add additional context in comments.

6 Comments

dear echo_Me "ON mv.tmax = mt.tmax" the mt.tmax is null, so this will not update the table.
I think only mt.transaction_date and mv.date can be join.
you right, then you need to make ON condition with the relation between the two tables.
you have no relation between tables ?
Before edit the above answer solve the problem, I changed the ON to ON STR_TO_DATE(mv.Date, '%m/%d/%Y') = mt.accident_dt UPDATE mytable mt INNER JOIN myvalues mv ON STR_TO_DATE(mv.Date, '%m/%d/%Y') = mt.accident_dt SET mt.tmax = mv.tmax , mt.tmin=mv.tmin WHERE Longitude_MAX >= v_lng and STR_TO_DATE(Date, '%m/%d/%Y') = v_date AND pkid = v_pkid;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.