MySQL Trigger Validate Before Insert
I think you are on the right track. One thing though is that you need to handle the case when there is no match. Assuming someone adds a row like:

(4, 40, 0, 0, 1000000000)


Your IF statement will evaluate to:

IF ( 1000000000 != null ) THEN ...
IF ( null ) THEN ...


i.e. this will be allowed.

If you add a create table statement and some insert statements, together with info on whether they should be allowed or not I'm pretty sure someone can come up with a suggestion.