3

I'm basically trying to build a query which looks something like:

UPDATE table_a, table_b
SET table_a.value = table_b.value
WHERE table_a.id = table_b.id;

It seems that a query like this is functionally equivalent:

UPDATE table_a
INNER JOIN table_b
  ON table_a.id = table_b.id
SET table_a.value = table_b.value;

The thing is that I can't figure out how the UpdateQuery allows me to attach a second table like that. I tried just adding a ->join('table_b') to my update query object, but I'm told that method is undefined. Argh.

I've ended up cheating by using db_query(), though I'm still curions to know if there's a way to do this properly using the API.

  • are you using D6 or D7? – saadlulu Oct 4 '12 at 9:09
  • D7. (Tag added.) – Garrett Albright Oct 4 '12 at 11:35
  • I faced the same problem. Seems there is no join() method for UpdateQuery. – milkovsky Sep 2 '14 at 19:23
0

Given the specific nature of your SQL statement, it would appear db_query() is the way to go. Just be sure to do it as an update hook in your .install (so you can drush updb) to implement smoothly.

  • You mean use hook_update_N()? I'm not sure if that was relevant to my issue (note that this question is nearly two years old)… but at any rate, yeah, I guess my suspicions are confirmed and cheating with db_query() is all I could do in this case. – Garrett Albright Sep 2 '14 at 21:01
  • Yes, I meant hook_update_xxx() @GarrettAlbright – Bob Liu Sep 3 '14 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.