tags:

views:

15

answers:

1

I have two MySQL tables, here's how they are structured:

table foo(
 foo_id varchar(32),
 field1 varchar(32),
 field2 varchar(32),
 bar_id varchar(32) 
);

table bar(
  bar_id varchar(32),
  field1 varchar(32),
  field2 varchar(32)
);

I would like to update the foo table to have the same values for field1 and field2 as the bar table. I've tried the following two queries, both of which run without error, but don't get me the results I want:

UPDATE foo LEFT JOIN bar ON foo.bar_id = bar.bar_id 
SET foo.field1 = bar.field1 AND foo.field2 = bar.field2;

also

UPDATE foo,bar SET foo.field1 = bar.field1 
AND foo.field2 = bar.field2 WHERE foo.bar_id = bar.bar_id

but neither work. what am I missing here?

+1  A: 

I think maybe you have problems with AND; should be comma instead. This way I think you are updating foo.field1 to become (bar.field1 AND foo.field2 = bar.field2) (logical conjunction between non-zeroness of bar.field1 and equality of field2-s), and I'm pretty sure that's not what you meant. Didn't test though; might be wrong.

Amadan
you're right, that was the problem. Though I find it odd that that wasn't a syntax error of somesort in MySQL. I'm assuming it thought I meant SET foo.field1 = (bar.field1 AND foo.field2 = bar.field2) and set the value to a boolean
GSto
MySQL has fairly permissive implicit casting, and `=` works for both assignment and comparison in SQL, so... no, no error. :)
Amadan