tags:

views:

45

answers:

4

is there any way i can group several of my columns together?

in one for my scripts i need to set several of my columns to the same value, is there a better way than typing out all the column names in my update query?

thanks

A: 

You'll need to type each column's name, there is no workaround.

UPDATE  mytable
SET     col1 = 1,
        col2 = 1,
        …
Quassnoi
A: 

No, this is not possible.

See the syntax for update:

...
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}
...
Oded
A: 

Update syntax for MySQL requires each column to be named explicitly

update table set a = 'value', b = 'value'
Trevor
+1  A: 

If these columns MUST always have the same value (that seems pointless) you could set an UPDATE TRIGGER on the column

delimiter |
CREATE TRIGGER somename BEFORE INSERT ON table1
 FOR EACH ROW BEGIN
  SET table1.col2=NEW.col1, table1.col3=NEW.col1;
END;

delimiter ;

But if that was the case, it would really make sense to just use ONE column instead of three.

Or, if its a Rights issue (this user shouldn't have the permission to make the columns different) you could build a Stored Procedure for doing the update

delimiter |
CREATE STORED PROCEDURE somename(IN val INT,IN whereval INT)
 BEGIN
  UPDATE table1 SET table1.col1=val, table1.col2=val, table1.col3=val
    WHERE table1.id=whereval;
END;

delimiter ;
MindStalker
the columns don't always gave the same value, but quite often i need to reset the values of about 40 columns (4 groups of 10) to the same value, so some sort of grouping would be useful. i now see i could use a stored procedure to do this :) thanks
Juddling