Du kan få den next
auto-increment id ved at bruge
SHOW TABLE STATUS FROM tablename LIKE Auto_increment
/*or*/
SELECT `auto_increment` FROM `INFORMATION_SCHEMA.TABLES` WHERE table_name = 'tablename'
Dette vil give dig den næste auto_increment-værdi.
Lav derefter en trigger før indsættelse:
DELIMITER $$
CREATE TRIGGER bi_table1_each BEFORE INSERT ON table1 FOR EACH ROW
BEGIN
DECLARE next_id integer;
SELECT `auto-increment` FROM `INFORMATION_SCHEMA.TABLES` INTO Next_id
WHERE TABLE_NAME = 'table1' LIMIT 1;
SET new.sortcolumn = next_id;
END $$
DELIMITER ;
Links
http://dev.mysql .com/doc/refman/5.1/da/show-table-status.html
http://dev.mysql.com /doc/refman/5.0/da/tables-table.html
http://dev.mysql.com/doc /refman/5.0/da/triggers.html
http://dev.mysql.com /doc/refman/5.0/da/create-trigger.html