MySQL update event

1

I asked this question yesterday, I got the answer I wanted, I'll be direct and challenge:

I have the query:

UPDATE users SET time = time - 1

time is of type int and receives as default value 0 , in my PHP code I made a schema where the user clicks the button adds 1800 to the value of time and the query event It starts at the beginning of the action, it happens that when it arrives at 0 it starts counting -1, -2, -3 ... 1800, etc.,

How can I do this?

    
asked by anonymous 03.08.2017 / 07:35

1 answer

2

You can do it in two ways as I see it.

  • Update whenever the WHERE clause returns true for the time > 0 condition. Example: UPDATE tabela SET time = time - 1 WHERE time > 0 .

  • Update your table so that the time field has the UNSIGNED attribute. This means that your field can only have non-negative numbers, according to the MySQL documentation. Example: ALTER TABLE tabela MODIFY COLUMN time int(10) UNSIGNED ZEROFILL .

  • See: UNSIGNED and ZEROFILL: What are the columns for MySQL

        
    03.08.2017 / 07:52