An INSERT conditioned on COUNT
        Posted  
        
            by 
                Anders Feder
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Anders Feder
        
        
        
        Published on 2010-12-28T02:36:38Z
        Indexed on 
            2010/12/28
            2:54 UTC
        
        
        Read the original article
        Hit count: 392
        
How can I construct a MySQL INSERT query that only executes if the number of rows satisfying some condition already in the table is less than 20, and fails otherwise?
That is, if the table has 18 rows satisfying the condition, then the INSERT should proceed. If the table has 23 rows satisfying the condition, then the INSERT should fail.
For atomicity, I need to express this in a single query, so two requests can not INSERT at the same time, each in the 'belief' that only 19 rows satisfy the condition.
Thank you.
© Stack Overflow or respective owner