Hi.

On Mon, Apr 29, 2002 at 10:00:07AM +0000, [EMAIL PROTECTED] wrote:
> 
> hi everybody,
> 
> I have problem with inserting data into table.
> MY table is:-
> Name  type
> ID    int(9)autoincrement
> Total int(9)
> 
> while i insert the data,i want the total(ID+2).Its mean the record should 
> be like this:-
> 
> ID Total
> 1   3
> 2   4
> 3   5
> 
> is it possible....i have try is like this
> mysql>insert into TableName(total) values(ID+2);

You cannot reference columns from within an INSERT clause.

You could do it in two steps:

INSERT INTO TableName (total) VALUES (2);
UPDATE TableName SET total=total+LAST_INSERT_ID() WHERE ID=LAST_INSERT_ID();

or

SELECT @next = MAX(ID)+1 FROM TableName;
INSERT INTO TableName (total) VALUES (@next+2);

> The result is like this:-
> ID Total
> 1   2
> 2   2
> 3   2
> 
> its cannot works....is there any ideas..........please help me....

Well, if you need always need this, your table design is IMHO flawed.
The total column would be redundant, because you could always select
(ID+2).

Bye,

        Benjamin.


-- 
[EMAIL PROTECTED]

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to