Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to add 1 to current field value with MySQL, but can't figure out what's wrong with my syntax

Tags:

sql

mysql

I'm hoping someone can spot my mistake in this MySQL query.

UPDATE 'databasename'.'tablename' 
   SET fieldB = fieldB + 1 
 WHERE fieldA = '2';

I'm basically trying to add 1 to the current value of fieldB where fieldA is i.e 2.

like image 366
Kanonskall Avatar asked Dec 02 '10 01:12

Kanonskall


People also ask

How do I add values to a specific column in MySQL?

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.

What does WHERE 1 mean in MySQL?

In MySQL “Where 1=1” results in all the rows of a table as this statement is always true. An example to better unerstand this statement is given as follows − First, a table is created with the help of the create command.

How do you check if a value is present in a column in MySQL?

preferred way, using MySQLi extension: $mysqli = new mysqli(SERVER, DBUSER, DBPASS, DATABASE); $result = $mysqli->query("SELECT id FROM mytable WHERE city = 'c7'"); if($result->num_rows == 0) { // row not found, do stuff... } else { // do other stuff... }


1 Answers

Single-quotes are for strings.

UPDATE `databasename`.`tablename` 
SET fieldB = fieldB + 1 
WHERE fieldA = '2';

You can use backticks around the database and tablenames; they aren't strictly necessary though.

like image 178
mechanical_meat Avatar answered Oct 07 '22 21:10

mechanical_meat