I\'m designing a comments MySQL db, and my comments table has fields:
id
primary key, auto-incrthread
int, not null
Do I have to use 2 queries?
Yes. As you discovered, the id value hasn't been generated yet in a BEFORE INSERT trigger. But you can't change your NEW.thread value in an AFTER INSERT trigger.
You can't rely on reading the INFORMATION_SCHEMA, because you can cause a race condition.
You'll just have to do the INSERT, and then immediately execute:
UPDATE comments SET thread=id WHERE id=LAST_INSERT_ID() AND thread IS NULL;
If it's a root comment.
See also my past answers on the similar topic: