inserting unique value

I need to insert a unique value into a (not auto-increment) column.
I try

insert into idtest (val) values ((select max(val) from idtest)+1);

but I get

ERROR 1093 (HY000): You can't specify target table 'idtest' for update
in FROM clause

- what is the correct way to do this?

Regards,
Rune
Rune Zedeler [ Do, 21 Juni 2007 15:20 ] [ ID #1744807 ]

Re: inserting unique value

== Quote from Rune Zedeler (rz [at] daimi.au.dk)'s article
> I need to insert a unique value into a (not auto-increment) column.
> I try
> insert into idtest (val) values ((select max(val) from idtest)+1);
> but I get
> ERROR 1093 (HY000): You can't specify target table 'idtest' for update
> in FROM clause
> - what is the correct way to do this?
> Regards,
> Rune

how about this:

insert into idtest (val) (select (max(val)+1) from idtest);

let me know if it works for you.
--
POST BY: lark with PHP News Reader
Shawn Hamzee [ Fr, 22 Juni 2007 16:42 ] [ ID #1745646 ]

Re: inserting unique value

lark skrev:

> insert into idtest (val) (select (max(val)+1) from idtest);

Thanks, works! :+)

-Rune
Rune Zedeler [ Fr, 22 Juni 2007 18:24 ] [ ID #1745647 ]
Datenbanken » mailing.database.mysql » inserting unique value

Vorheriges Thema: SQLMAIL for MYSQL ?
Nächstes Thema: creating table for rails