Inserting data into a table (subselect?)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Templeton Peck
Forum Commoner
Posts: 45
Joined: Sun May 11, 2003 7:51 pm

Inserting data into a table (subselect?)

Post 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.
toantt
Forum Newbie
Posts: 6
Joined: Sat May 17, 2003 11:31 pm

Post 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)");
Post Reply