Updating an entire row with values from a row on a different table (cake/mysql)
currently I use a query like this to update a table called cats from a table called admin cats (the $id is because it's in cake, it's just a number like 1,2,3 etc). As it is I name every field (there are maybe 50) and say it like this:
update cats AS c, admin_cats set c.category=(select category from admin_cats where id=$id), [--removed 50 columns in the middle--] c.overviewImageT开发者_开发技巧ext8=(select overviewImageText8 from admin_cats where id=$id) where c.id = $id");
I'm updating every column, so is there a way I can just say something like:
UPDATE cats SET * = SELECT * FROM admin_cats WHERE admin_cats.id = $id) WHERE cats.id IN ($id);
Of course I tried that and it doesn't work, but I want to do it where the cats row where the id matches $id will update with values from the admin_cats that has that same id. How can I do this?
For MySQL specifically, use REPLACE
REPLACE cats (<optional columns>)
SELECT <cols>
FROM admin_cats
WHERE admin_cats.id = $id;
Note: This assumes cats has a Primary Key (e.g. cats.id) that the duplicate row error can be caught, and the row replaced.
Note: other databases don't support replace or use other, similar but different syntax.
精彩评论