UPDATE with logical AND operator - mysql

I tried running this sql
UPDATE table
SET read = 1
WHERE col1_id = 2
AND col3_id = 1;
and it return an error (error in your sql syntax)
#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 'read = 1 WHERE team_notification_id = 2 AND listener_id = 1' at line 1
But when I used thesame WHERE and AND :), in a SELECT statement no error was returned.
SELECT *
FROM read
WHERE col1_id = 2
AND col3_id = 1
please what did I do wrong, I am no seeing it.

After keyword UPDATE there should be a table name. If your table has actually name table (that's a bit strange) you should escape it to let MySQL know it's not a keyword "table" but actual table name.
The same thing is about read.
Both table and read are MySQL keywords (the full list is here http://dev.mysql.com/doc/refman/5.7/en/keywords.html) so if it's your actual table and column names then you should escape it.
Better to escape all table and column names to prevent issues like that.
Escaping in MySQL is done with backtick symbol ` so your query will looks like:
UPDATE `table`
SET `read` = 1
WHERE `col1_id` = 2
AND `col3_id` = 1;

UPDATE tablename
SET `field_name` = 1
WHERE col1_id = 2
AND col3_id = 1;

UPDATE table
SET [read] = 1
WHERE col1_id = 2
AND col3_id = 1;

Related

Getting error using EXECUTE('UPDATE..') on a MySQL server from SQL Server

I intend to do:
EXECUTE('UPDATE tableA SET campaignkey = ''20170101'' where storekey = 16
and campaignkey LIKE ''%,%''') at MYLINKEDSERVER
but I get the error that:
You have an error in your SQL syntax; [...] for the right syntax to use near 'where storekey = 16 and campaignkey LIKE '%,%''
Does anyone have any idea what is wrong? To me it seems like I might have one ' too many on my LIKE statement, but I have Always used '' to indicate a non-numeric value. I don't want to fiddle with this to prevent updating far too many values on this server.
campaignkey is non-numeric (I Believe varchar) and storekey is integer.
Edit
I must not use OPENQUERY() because it is not set up correctly, and this is a urgent update.
Edit 2
Seems like it is because of apostrophes 's in the EXECUTE statement.
When I conduct:
select * from openquery(linkedserver,'Select * from tableA where storekey = 16
and campaignkey = ''20170826,151''')
it works, but when using:
EXECUTE('Select * from tableA where storekey = 16
and campaignkey = ''20170826,151''') at linkedserver
I get the error that I need to check the manual by the where clause. From googling it appears however that the correct syntax in fact is:
EXECUTE('UPDATE TableX SET StringVar = ''stringValue'' WHERE intVar = 3
AND stringVar = ''xxxxx''') AT LinkedServer
I don't know why this won't work for me.. I have tried many combinations of '', '" etc.
What about this one?
update openquery(linkedserver,'Select * from tableA where storekey = 16
and campaignkey = ''20170826,151''')
set campaignkey = '20170101'

mySQL UPDATE failed. What is wrong with this mySQL UPDATE query?

here's the generated query:
UPDATE namelist
SET 'submitterName' = 'Jim'
,'actorName' = 'dingle'
,'setYear' = '1103'
,'country' = 'tanata'
,'blink' = 'on'
,'crush' = 'on'
,'initialize' = 'on'
,'entered' = 'on'
,'stuck' = 'on'
,'catapult' = 'on'
,'ruck' = 'on'
WHERE id = 31
it generates this (less than helpful) error:
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 (query snippet) at line 1
for the life of me, i can't spot where the problem is. yes, column names match, yes
TIA for helping out.
WR!
You have used single quotes before and after columns in the query, replace those single quotes with backquotes.
So the query like
UPDATE namelist
SET `submitterName`='Jim',
`actorName`='dingle',
`setYear`='1103',
`country`='tanata',
`blink`='on',
`crush`='on',
`initialize`='on',
`entered`='on',
`stuck`='on',
`catapult`='on',
`ruck`='on'
WHERE id=31;
user ` instead of '
like this
UPDATE namelist SET `submitterName`='Jim',`actorName`='dingle',`setYear`='1103',`country`='tanata',`blink`='on',`crush`='on',`initialize`='on',`entered`='on',`stuck`='on',`catapult`='on',`ruck`='on' WHERE id=31

How to update Sphinx Realtime Index with field = old_value + new_value?

I am trying to update the Sphinx Search Realtime Index , using values from my Mysql Table.,
I want to add the new value in the old value of the RT index's record like this ,
i want to achieve this
UPDATE RT_index SET col1 = old_val + new_val WHERE id = xx ;
query i am trying is
UPDATE RT_index SET comments_count = comments_count + 3 WHERE id = 1157642
but Sphinx giving me errror
ERROR 1064 (42000): sphinxql: syntax error, unexpected IDENT,
expecting CONST_INT (or 4 other tokens) near 'comments_count + 3 WHERE
id = 1157642'
i have tried query like this
UPDATE RT_index SET comments_count = value(comments_count) + 3 WHERE id = 1157642;
but still sphinx gives error,
ERROR 1064 (42000): sphinxql: syntax error, unexpected IDENT,
expecting CONST_INT (or 4 other tokens) near 'value(comments_count) +
3 WHERE id = 1157642'
How can i add new value in old value using update in sphinx real-time index ?
i am using PHP to do this.
there is not much info about it in http://sphinxsearch.com/docs/current.html#sphinxql-update
You can't. Need to first run a SELECT query to get the current value, and then run the UPDATE.
Not sure if can use a transaction to make the update atomic.

Update table using GUID

I am using MySQL with ASP.NET/VB. In a table I use GUID instead of int identifiers. All goes as planned until I try to update a specific row, where I get a syntax error in the statement below:
Dim q As String = "UPDATE documents SET date_document = #date_document, document_type = #document_type, sender = #sender, receiver = #receiver, description = #description, document_number = #document_number, pages = #pages, handled_date = current_timestamp, handled_user_id = #handled_user_id, error_code = #error_code) WHERE id = #id"
My GUID parameter:
.Parameters.Add("#id", MySqlDbType.Guid, 16).Value = myguid
And the error:
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 ') WHERE id = '8873442f-2f0b-4372-ac08-8388220c6eca'' at line 1
Any ideas on what's going on?
You're chasing down the wrong issue. What character does your error syntax begin with? It starts off as ') Where id = ...
You're assuming it's the id. It's not. That works fine. The first character is a closing parenthesis. That's the clue. There is no opening parenthesis. Remove the ) because you don't need it with an update statement.

How to use concat() to append to multiple columns in a single mysql update

I can't figure out the syntax for Mysql update with multiple concatinations. I want to be able to append a string to the end of the string stored in the database but do it to multiple columns all at once. I can do one column at a time just fine with this
UPDATE `table1`.`column1` SET `category1` = CONCAT(category1,'$value[0]',) WHERE `id`='$id';
But when I try to do it to multiple columns in the same table I get a syntax error.
UPDATE `table1`.`column1`
SET `category1` = CONCAT(category1,'5'),
`category2` = CONCAT(category2,'5'),
`category3` = CONCAT(category3,'5'),
`category4` = CONCAT(category4,'5'),
`category5` = CONCAT(category5,'5'),
`comments` = CONCAT(comments, 'jfsaklfsad')
WHERE `for_student_id`='46';
"You have an error in your SQL syntax;"
I can't find the syntax for separating each concat.
According to MySQL docs, UPDATE does not support such syntax. You must reference the table name, without the column, before the SET:
UPDATE `table1`
SET `category1` = CONCAT(category1,'5'),
`category2` = CONCAT(category2,'5'),
`category3` = CONCAT(category3,'5'),
`category4` = CONCAT(category4,'5'),
`category5` = CONCAT(category5,'5'),
`comments` = CONCAT(comments, 'jfsaklfsad')
WHERE `for_student_id`='46';