Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 1 to a field

Tags:

How do I turn the following 2 queries into 1 query

$sql    = "SELECT level FROM skills WHERE id = $id LIMIT 1;"; $result = $db->sql_query($sql); $level  = (int) $db->sql_fetchfield('level'); $db->sql_freeresult($result);  ++$level;  $sql    = "UPDATE skills SET level = $level WHERE id = $id;"; $result = $db->sql_query($sql); $db->sql_freeresult($result); 

I'm using it in a phpBB mod but the gist is that I grab the level, add one to it then update, it seems that it'd be much easier and faster if I could do it as one query.

Edit: $id has already been forced to be an integer, thus no escaping is needed this time.

like image 418
Teifion Avatar asked Aug 08 '08 12:08

Teifion


People also ask

How do you increment a number by 1 in SQL?

Syntax for MySQLMySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. VALUES ('Lars','Monsen'); The SQL statement above would insert a new record into the "Persons" table.

How do I add a number to a column in MySQL?

In syntax, First, you must specify the name of the table. After that, in parenthesis, you must specify the column name of the table, and columns must be separated by a comma. The values that you want to insert must be inside the parenthesis, and it must be followed by the VALUES clause.

How do you add one variable in SQL?

+= (Addition Assignment) (Transact-SQL) Adds two numbers and sets a value to the result of the operation. For example, if a variable @x equals 35, then @x += 2 takes the original value of @x, add 2 and sets @x to that new value (37).

How to make a column AUTO increment in MySQL?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name.


1 Answers

I get downmodded for this?

$sql = "UPDATE skills SET level = level+1 WHERE id = $id"; $result = $db->sql_query($sql); $db->sql_freeresult($result); 

In Teifion's specific case, the phpBB DDL lists that particular field as NOT NULL, so there's no danger of incrementing NULL.

In the general case, you should not use NULL to represent zero. Incrementing NULL should give an answer of NULL. If you're the kind of misguided developer who thinks NULL=0, step away from keyboard and find another pastime, you're just making life hard for the rest of us. Of course, this is the computer industry and who are we to say you're wrong? If you're not wrong, use

$sql = "UPDATE skills SET level = COALESCE(level,0)+1 WHERE id = $id"; 

...but let's face it: you're wrong. If everyone starts at level 0, then your DDL should include

level INT DEFAULT '0' NOT NULL 

in case the programmers forget to set it when they create a record. If not everyone starts on level 0, then skip the DEFAULT and force the programmer to supply a value on creation. If some people are beyond levels, for whom having a level is a meaningless thing, then adding one to their level equally has no meaning. In that case, drop the NOT NULL from the DDL.

like image 140
Josh Avatar answered Oct 02 '22 12:10

Josh