SQL query doesn't work. Possible syntax error - mysql

Seems to be strange but very simple SQL query doesn't work for me.
UPDATE wp_postmeta SET `meta_value` = REPLACE(post_meta, `http://url/`, `http://new_url`)
Error message appears 'a new statement was found but no delimiter was found between it and the previous one (REPLACE) and when I'm trying to execute the code - #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 ')' at line 1. Please advise.

Your specific issue is the use of backticks rather than single quotes. However, I think a better way to express the logic is:
UPDATE wp_postmeta
SET meta_value = CONCAT('http://new_url', SUBSTRING(post_meta, 12) )
WHERE meta_value LIKE 'http://url/%' AND
meta_key = ??;
This uses the WHERE clause to filter out rows that should not be updated. It also ensures that the update is only to the occurrence of 'http://url/' at the beginning the string -- usually the logic that is intended.

Related

MySQL find and replace not working as expected (serialized data)

Im trying to create a MySQL search and replace script for my wp_postmeta db table.
I need to search this fields that are similar to this.
a:2:{i:0;a:3:{s:4:"name";s:15:"Personalisation";s:4:"type";s:7:"heading";s:11:"description";s:0:"";}i:1;a:13:{s:4:"name";s:8:"Initials";s:12:"title_format";s:5:"label";s:4:"type";s:11:"custom_text";s:17:"restrictions_type";s:8:"any_text";s:11:"description";s:0:"";s:18:"description_enable";i:0;s:8:"required";i:1;s:3:"min";s:1:"1";s:3:"max";s:1:"4";s:12:"restrictions";i:1;s:5:"price";s:1:"3";s:10:"price_type";s:14:"quantity_based";s:12:"adjust_price";i:1;}}
and replace the "required";i:1; with "required";i:0;
Im using phpmyadmin to create and run the script but so far I cannot get what I have written to run with the semicolons in there.
This is what ive come up with so far;
UPDATE wp_postmeta
SET meta_value = replace(meta_value, '"required";i:1;', '"required";i:0;')
WHERE meta_key = '_product_addons';
and get the error (removing the semicolons fixes this but then doesn't match anything):
#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 ''"required")' at line 1
I've tried using \ to escape them and this doesn't work, this is about as far as my knowledge goes with MySQL so hoping someone can help?
Thanks
OK so I've found out that this actually works fine but won't work when using the Simulate option, can anyone explain why this is?

MySQL replace QUOTE

I need to replace &quote; in a string. I tried to do this:
SET `title` = REPLACE( `title`, '"', '' )
but it gives me a parsing error.
This is the error:
#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 ''&quot)' at line 1
Server version: 5.5.57-cll - MySQL Community Server (GPL)
How do I do this?
Your query should work, but it seems like the error is from another query. Anyhow:
Try this:
UPDATE tbl_name
SET
field_name = REPLACE(field_name,
string_to_find,
string_to_replace)
WHERE
conditions;
Example:
UPDATE bbb_sefurls
SET
metatitle = REPLACE(metatitle,
'&quote;',
'');
No need for a condition
This is simple approach but it replaces all the " in string.
UPDATE dummy_tab SET metatitle =REPLACE(metatitle,'/"','') WHERE metatitle LIKE '%"'
If I'm to read your error 100% as written... you have the word quote spelled wrong some where.
...for the right syntax to use near ''&quot)'
notice in your error it shows no "e" There for your replace statement would also NOT catch this.
Or more closely looking at the image you posted... you are replacing
&quote(semicolon)
with
''
But the error says it is finding the string
&quote)
somewhere in your query... Which would seem to be invalid.
Search your code for
&quot)

Correct format to use regex in MySQL SELECT statement (not within WHERE section)

I need to use REGEX (or similar functiuonality) to update an existing table in order to identify if a string field contains a correctly formatted code:
UPDATE tblRequests SET flagIsRefCodeOK=(RefCode REGEX '^[A-Z0-9]{8}-(?:[A-Z0-9]{4}-){3}[A-Z0-9]{12}$') WHERE DataSetID=11
But it doesn't seem to like the statement:
#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 'REGEX '^[A-Z0-9]
Note, this code will be added to a BEFORE INSERT trigger, which is responsible for updating a number of flags. I'm not fussed (much) with whether the REGEX is correct, especially within MySQL, I just want it to try to work. Then I'll figure out the exact REGEX if this doesn't work.
Thnx
The operator name is REGEXP not REGEX, so try:
UPDATE tblRequests
SET flagIsRefCodeOK= (RefCode REGEXP '^[A-Z0-9]{8}-(?:[A-Z0-9]{4}-){3}[A-Z0-9]{12}$')
WHERE DataSetID=11;

MySQL query sanity check, for update query

I have this query
update user_remember_me set
when='2012-07-06 05:44:27',
hash='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7'
where user = '21';
and I am getting this error
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 'when='2012-07-06 05:44:27', hash='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7' wher' at line 1
Im failing to miss the connection here, I've used simple updates like this everywhere without issue til this, maybe Im getting to tired, but this is gonna drive me nuts til I have an answer
When is a key word in mysql, change the column name
or you can use it as
`when`='2012-07-06 05:44:27'
when is a reserved word in mysql
update user_remember_me set
`when`='2012-07-06 05:44:27',
`hash`='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7'
where user = '21';
So you must backtick your column
when is a keyword within MySQL. You have to escape it, if you want to use it as a column identifier (as you should with all column identifiers!):
UPDATE user_remember_me
SET
`when`='2012-07-06 05:44:27',
`hash`='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7'
WHERE `user` = '21';

Error while changing paths in MySQL

Hi iam trying to change the path in some links in my WordPress database.
In my table wp_commeentmeta, I am using the syntax:
UPDATE table SET meta_value = REPLACE(meta_value, 'http://articles.mydomain.com', 'http://localhost/articles')
BUT I get the following ERROR:
,#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 SET meta_value = REPLACE(meta_value,
'http://articles.mydomain.com', 'http' at line 1
Any help on this please?
Please note : I am using phpMyAdmin for this.
Change the table to be the name of your table
UPDATE wp_commeentmeta
SET meta_value = REPLACE(
meta_value,
'http://articles.mydomain.com',
'http://localhost/articles'
);
(Just fyi, the linebreaks aren't important, just makes the query more readable)