Page 1 of 1

Inserting data into a table (subselect?)

Posted: Thu Jun 05, 2003 3:14 pm
by Templeton Peck
insert into enrolls (section_id,total)
values(324,max(sub) +.1);

how can I make it work so the value for the total of this entry is the max of the sub column from the same table + .1

i've tried: values(324,(SELECT max(sub) FROM enrolls) +.1);

but it does not work because its against the rules to take the max from the same table your inserting into.

Posted: Fri Jun 06, 2003 10:52 pm
by toantt
I think you should using code in php because a complex query will slower excute than simple query:

Code: Select all

$rs = mysql_query("Select Max(sub) from enrolls");
list($maxVal) = mysql_fetch_row($rs);

mysql_query("Insert into enrolls (section_id,total) values(324,($maxVal +.1)");