I am trying to replace some sentences from wordpress posts content using phpMyadmin
here is my update query
UPDATE `wp_posts` SET `post_content` = REPLACE (`post_content`, 'Example Routes:', 'Routes:') WHERE `post_content` LIKE '%Example Routes:%';
but it says "0 rows affected".
I have run a select query that have the same condition to ensure that there is resutls and I have so many resutls
here is the select query
SELECT * FROM `wp_posts` WHERE `post_content` LIKE '%Example Routes:%';
Please note that both query conditions is the same as what I am updating my condition is "LIKE '%Example Routes:%'" and the update is "REPLACE (post_content, 'Example Routes:', 'Routes:')".
Any idea why the update is giving me 0 rows affected?
Try dropping the space between REPLACE and the opening parenthesis:
UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`, 'Example Routes:', 'Routes:')
WHERE `post_content` LIKE '%Example Routes:%';
By default, you aren't allowed to include whitespace between function names and their parentheses: http://dev.mysql.com/doc/refman/5.7/en/functions.html
After a lot of search and working around I figured it out, the select query give results because the LIKE operator is not case sensitive but Replace is, so I needed to replace 'Example Routes:' to 'Example routes:' in the replace function which is a bit tricky.
I ended up using this query which works for me
UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`,'Example routes:','Routes:')
WHERE `post_content` LIKE '%Example Routes:%';
Related
I'd like to make union query with Laravel Eloquent.
When we proceed UNION queries, there should be the same number of selected columns in both queries.
To skip that rule I would like to select NULL as Column_name,
but Laravel API automatically replaces NULL with 'Null' and that causes an error "Null column is not existed".
How to remove these automatically added quotes from Null?
That is what I have:
The first query:
...->select("Calendars.*","Services.Id as IdService","Services.Name as ServiceName","NULL as Price")
The second query:
...->select("Calendars.*","Services.Id as IdService","Services.Name as ServiceName","PaidService.Price")
Result is:
...union (select `Calendars`.*, `Services`.`Id` as `IdService`, `Services`.`Name` as `ServiceName`, `NULL` as `Price` from `Calendars`
Thanks a lot!
Consider using DB::raw for this. It will stop laravel from modifying the statement and parse it as is.
DB::raw("NULL as Price")
which will make the first query
...->select("Calendars.*",
"Services.Id as IdService",
"Services.Name as ServiceName",
DB::raw("NULL as Price"))
Im trying to set a variable using sprintf via a Join Query, then update each time this appears in the table using the ID loaded into the query.
The update works fine if I use a slightly different query but one that gives an identical set of results, so have come to the conclusion Im doing something silly.
Here is a selection below:
//below is a simpler version of what I would like to work with the update
$sql=sprintf("SELECT `test`.`id` FROM `test` JOIN `test2` ON (test.agent=test2.user) WHERE `test`.`type`='new' AND `test2`.`note` = 'p';
//this is what works, even though the output (list of test.id) is identical to above
$sql=sprintf("SELECT `id` FROM `test` WHERE `test`.`type`='new' AND `agent`='test.user';
//here is my update that works with the second select
if($ref_id = mysql_one_data($sql)){
$updateSQL= sprintf("UPDATE `test` SET `type`='testdata', `priority`=%s, `note`=%s WHERE `id`=%s;",SQLVal('100', "int"),SQLVal($note, "text"),SQLVal($ref_id, "int"));
$result = mysql_query($updateSQL) or die(mysql_error());
$processed=TRUE; $result="updated";
$count_converted++;
}
Any ideas? I'm at a total loss! As I said both queries give 100% the same output, so the variable produced should be the same right? and so if one update works when it finds a corresponding value the other should too.
Excuse me if I ask the obvious, but I am quite mySQL illiterate .
I am referring to a standard wordpress DB install, although this is not a wordpress specific question, but more mySQL general knowledge ..
If I want to change all the values of a certain field across all posts , I usually do :
UPDATE `wp_postmeta` SET `meta_value` = replace(meta_value, 'old_value', 'new_value') WHERE `meta_key` LIKE 'my_meta_key'
The problem is as follows :
In that specific DB , the INITIAL values were inserted with a white space prefix and suffix , e.g. :
"city name"
was actually inserted as
" city name "
of course , being a PHP person, I immediately thought of the trim() command - which I found out exists also in mySQL.
SO now I thought I could just do
UPDATE `wp_postmeta` SET `meta_value` = TRIM('meta_value') WHERE `meta_key` LIKE 'my_meta_key'
But It is not working ..
What Am I doing wrong ? I know that mySQL is "ticks sensetive" ( so to speak ) But I tried both 'meta_value' and `meta_value' ( sorry, markdown limitations seems to truncate the backticks)
Doing replace(meta_value, ' ', '') is dengaurous because some cities havea spaces in their name, and LTRIM RTRIM is somewhat not working for me as well
I also know there is a way to set a variable in sql ( #my_var )
but what is the correct syntax of doing that in my case ?
SET #my_var = `meta_value`
and then
UPDATE `wp_postmeta` TRIM(#my_var) WHERE `meta_key` LIKE 'my_meta_key'
I of course know how to fix it in a php loop (with trim() ) but I would like to learn what is the best way to do it in mySQL..
To refer to the column, you need backticks:
UPDATE `wp_postmeta`
SET `meta_value` = TRIM(`meta_value`)
WHERE `meta_key` LIKE 'my_meta_key';
Your expression TRIM('meta_value') is trimming the string 'meta_value', not the value in the column.
Good day.
I have row in database:
When i make first select:
SELECT * FROM `table` WHERE `name` Like '%Коляска \"Balmoral\" Silver Cross%'
i get null(num rows = 0)...
But if i make second select:
SELECT * FROM `products` WHERE `name` Like '%Balmoral%'
i get num rows = 1.
Why not work first select and how make right select?
Slashes in database and in select becose i use mysql_real_escape_string when i insert new row and use select
The backslash is an escape character. If you want to look for it you need to escape it. (In other words, you need to escape the escape character.):
SELECT * FROM `table` WHERE `name` Like '%Коляска \\"Balmoral\\" Silver Cross%
Use this:
SELECT * FROM `titles` WHERE `title` LIKE '%Коляска \\\\"Balmoral\\\\%" Silver Cross'
Explanation: the \ must be escaped with \ for SQL, but for PHP we need extra \ for escaping also.
Or Simply:
SELECT * FROM `titles` WHERE `title` = 'Коляска \\"Balmoral\\" Silver Cross'
However, I don't know why!!!
Like not good idea in this operation. Need use fulltext.
Example
SELECT *, MATCH (name) AGAINST
('Коляска \"Balmoral\" Silver Cross') AS score
FROM products WHERE MATCH (name) AGAINST
('Коляска \"Balmoral\" Silver Cross');
I have some rows ( > 50.000) in my database which contains columns with strings + '\n'. I mean the records looks like this:
abcd\n or \nabcd
Because of that sometimes my application does not work correctly. My question is: How could i update this rows with the correct value and eliminate '\n' spaces besides going to every row and update it manually ?
update your_table set somecolumn = replace(somecolumn, '\n', '')
update mytable SET title = TRIM(TRAILING '\n' FROM title)
Have you tried this?
UPDATE table SET column=REPLACE(column,'\\n','') WHERE column LIKE '%\\n' OR column LIKE '\\n%'
If you don't know where the new line is, simply:
UPDATE table SET column=REPLACE(column,'\\n','')