Update longtext field in mysql - mysql

Im trying to update a longtext type field called 'comment' using a simple sql query in mysql client like this :
Update mytable set comment='Test' where id = 1;
But i'm getting this error
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'comment='Test' where id = 1' at line 1
Am i missing something ?, thanks in advance.

comment is a reserved word, if you want to have a table/field with that name, you have to quote it (or use the table.fieldname syntax, in case of a field). default in mysql is the backtick for that, so:
update mytable set `comment`='Test' where id = 1;

Found it, it gets solved with this:
update mytable as a set a.comment='Test' where id = 1;

Related

mysql syntax error for update rows

update amazon-crawler set `flag_images`= '0' where `id`='966'
i get #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-crawler set flag_images= '0' where id='966'' at line 1
why syntax error?
amazon-crawler is the table
flag images and id are columns
My guess is that the hyphen in the table name is causing problems, because it is an arithmetic operator. Try also escaping the table name:
UPDATE `amazon-crawler` SET `flag_images`= '0' WHERE `id` = '966';
Note that you should try to avoid using backticks in your query, unless absolutely needed. Using backticks means that any name would potentially work, even one which happens to be a MySQL reserved keyword. Also, I'm guessing that flag_images and id are numeric columns, in which case you should be comparing them against numbers, not strings. So I would write your update as this:
UPDATE `amazon-crawler` SET flag_images= 0 WHERE id = 966;
Here, only the table name has to appear in backticks.

SQL syntax, cant update table

I am getting the error
Could not update users table: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Long = '-2.8867589' WHERE Username = 'test1'' at line 1"
when i run this sql statement:
if($updateuser=mysql_query("UPDATE Users SET Long = '$long' WHERE Username = '$_SESSION[myusername]'")){
echo 'Users table updated';
}
LONG is a reserved word in MySQL (see here). It is a bad name for a column, but if you use it, then you need backticks:
UPDATE Users
SET `Long` = '$long'
WHERE Username = '$_SESSION[myusername]';
You should also switch to mysqli and use parameterized queries.

MySQL query with enum values

I've the following structure in MySQL 5.6.19.
CREATE TABLE `metadata` (
`md5` char(32) NOT NULL,
`match` enum('none','md5','similarity') DEFAULT NULL
)
And I got an error doing a query like this:
select * from metadata where match = 'md5';
The error is:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '= 'md5'' at line 1
There're multiple entries in the table and rows that could match the query. But MySQL refuse to do it. Any idea about the reason?
Thanks!
MATCH is reserved keyword in MySQL: http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-5.html. You should enclose your field name in backticks to make it work:
select * from metadata where `match` = 'md5';

#1064 Syntax Error, Simple Search and Replace Command?

Switching Wordpress databases, and I am attempting to run the Search and Replace command to change all the permalinks.
use ruepi;
update [table_name] set [field_name] =
replace([field_name],'[http://131.193.220.64/ruepi]','[http://ruepi.uic.edu]');
I am getting back:
SQL query:
UPDATE [table_name] SET [field_name] = REPLACE( [field_name],
'[http://131.193.220.64/ruepi]', '[http://ruepi.uic.edu]' ) ;
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near '[table_name] set [field_name] =
replace([field_name],'[http://131.193.220.64/rue' at line 1
Not sure exactly where my syntax is wrong? If anyone could look over this real quick.
Edit: Still getting #1146 error, which is the same error I got when previously trying this command:
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'http://131.193.220.64/ruepi',
'http://ruepi.uic.edu/');
ERROR:
Error
SQL query:
UPDATE `table_name` SET `field_name` = 'http://131.193.220.64/ruepi' WHERE
`field_name` = 'http://ruepi.uic.edu';
MySQL said: Documentation
#1146 - Table 'ruepi.table_name' doesn't exist
This does not look like a valid mysql command i.e. using [] for the column names
Should be as
update table_name
set field_name =
replace(field_name,'http://131.193.220.64/ruepi','http://ruepi.uic.edu');
USE ruepi;
UPDATE `table_name` set `field_name` = 'http://131.193.220.64/ruepi'
where `field_name` = 'http://ruepi.uic.edu';
Try this.

delete query for mysql using c

can anyone tell me the correct query to delete values from mysql db table,in my case the table name and id are accepted from the user and the row is deleted based on id.This is my query
sprintf(Query,"DELETE FROM ('%s') where id = (%d)",tb1,idt1) ;
/*table name is in form of string and id is int */
mysql_query(conn,Query);
You should remove parentheses around the table name:
sprintf(Query,"DELETE FROM '%s' where id = (%d)",tb1,idt1) ;
MySQL considers queries like this syntax errors:
delete from (mytable) where id=2;
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(mytable) where id=2' at line 1
(I'll assume that you know everything about SQL injection attacks, and that neither tb1 nor idt1 are constructed from user input in any shape or form).