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

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.

Related

SQL syntax error - multiple values from two tables & insert total

I need some help with the query below - I am trying to pull information regarding price and multiply with the quantity & insert the sum into the table. So far I have,
update passenger_baggage
SET passenger_baggage.total_baggage_cost=passenger_baggage.passenger_baggage_quantity*baggage_type.baggage_type_cost
FROM passenger_baggage INNER JOIN baggage_type
ON passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id
WHERE passenger_id = "3";
and getting this error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'FROM passenger_baggage INNER JOIN baggage_type ON
passenger_baggage.passenge...' at line 3
Expecting the query to multiply the two values & insert the total.
There is no FROM clause in an UPDATE query.
Try with this modified query:
update passenger_baggage
INNER JOIN baggage_type
ON passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id
SET passenger_baggage.total_baggage_cost = passenger_baggage.passenger_baggage_quantity * baggage_type.baggage_type_cost
WHERE passenger_id = "3";
Try this:
UPDATE passenger_baggage, baggage_type
SET passenger_baggage.total_baggage_cost = passenger_baggage.passenger_baggage_quantity * baggage_type.baggage_type_cost
WHERE passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id AND passenger_id = "3";
Saw an example from the MySQL doc (https://dev.mysql.com/doc/refman/8.0/en/update.html)

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'

UPDATE with logical AND operator

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;

SQL error with variable assignment, concat, and right

I'm trying to generate an ID by concatenating bits from a few cells in a MySQL table. I want t0 get rid of - and : and only have digits in the ID. I get a syntax error with the following:
update scan_data
set #scanDate1 = replace(scanDate,'-','')
set #scanTime1 = replace(scanTime,'-','')
scanID = concat(right(scanContent,2),right(#scanDate1,2),right(#scanTime1,2))
What do I need to change?
Try this
update scan_data set scanID = concat(right(scanContent, 2),
right(replace(scanDate,'-',''), 2),
right(replace(scanTime,'-',''), 2)
);

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.