No way to replace string of unknown length with SQL query? - mysql

Is this even possible?
All I want to do is search for a common string in a Wordpress database column and delete the string AND everything to the left of the string.
I was trying variations of the following:
UPDATE wp_posts
SET post_content = REPLACE(post_content, '%(1280 x 853)</p>', '')
I've since realized that you can't use REPLACE with wildcards. So is there any other way to do it?
Note: The content to the left of the common string is of varying length, so I wouldn't be able to use any code that needs to specify a certain number of characters.
And yes, I actually want to update every applicable row in the database column, not simply return a SELECT statement.
Any help would be much appreciated. My brain aged 10 years over this problem.

Given this
MariaDB [sandbox]> SELECT * FROM T;
+---------------------------+
| STR |
+---------------------------+
| AB(1280 x 853)</p>CD |
| 1234(1280 x 853)</p>56789 |
| ZYX |
+---------------------------+
this
UPDATE T
SET STR = SUBSTRING(STR,INSTR(STR,'(1280 x 853)</p>') + LENGTH('(1280 x 853)</p>'),LENGTH(STR))
WHERE INSTR(STR,'(1280 x 853)</p>') > 0
;
results in
SELECT * FROM T;
+-------+
| STR |
+-------+
| CD |
| 56789 |
| ZYX |
+-------+

Hope I understand your question clearly, you can try this:
UPDATE wp_posts
SET post_content = SUBSTRING_INDEX(post_content, '(1280 x 853)</p>', -1);

Try this,
UPDATE wp_posts SET post_content = SUBSTR(post_content, 1, (LENGTH(post_content) - 16));
If any further improved query, I will post in the comment.

Related

MySQL string splitting on delimiters

Based on https://stackoverflow.com/a/59666211/4250302 I created the stored function get_enum_item for future processing the lists of possible values in the ENUM() type fields.
It works fine enough, but... but I can't determine what to do if the delimiter itself is the part of a string being split. For example:
(square brackets are for readability)
mysql> set #q=",v1,',v2'" --empty string, "v1", "comma-v2";
mysql> select concat('[',get_enum_item(#q,',',0),']') as item;
+------+
| item |
+------+
| [] |
+------+
it is OK
mysql> select concat('[',get_enum_item(#q,',',1),']') as item;
+------+
| item |
+------+
| [v1] |
+------+
it is also OK
mysql> select concat('[',get_enum_item(#q,',',2),']') as item;
+------+
| item |
+------+
| ['] |
+------+
It is not OK
the #q contains 3 commas, the first two of these are real delimiters, while the last one is the part of the third possible value: "comma-v-two". And I have no idea how to avoid confusion of splitting function. MySQL WorkBench in the "form editor" mode solves this trouble somehow, but how can I solve this with MySQL's code?
Well, I can rely on the fact that the show_columns-like queries show the enums in "hardcoded" manner:
select column_name,column_type
from information_schema.columns
where data_type='enum' and table_name='assemblies';
+--------------+------------------------------------------------------------------+
| COLUMN_NAME | COLUMN_TYPE |
+--------------+------------------------------------------------------------------+
| AssetTagType | enum('','И/Н','Н/Н',',fgg') |
| PCTagType | enum('','И/Н','Н/Н') |
| MonTagType | enum('','И/Н','Н/Н') |
| UPSTagType | enum('','И/Н','Н/Н') |
| OtherTagType | enum('','И/Н','Н/Н') |
| state | enum('в работе','на списание','списано') |
+--------------+------------------------------------------------------------------+
Thus I can try to use ',' as a delimiter, but this will not save me from the case if the "comma-apostrophe" combination is the part of possible value... :-(
The only thing I can imagine is to count apostrophes and if the delimiting comma is after the even number of ''s, then it is the delimiter, while if it follows an odd number of ''s, it is the part of the value.
And I can't invent anything except for dumb scanning the input string inside the loop. But maybe there are some other suggestions to get the values split correctly?
Please, don't suggest use PHP, Python, AWK, and so on. The query will be executed from the Pascal (Lazarus, CodeTyphoon) application, and calling external processors is highly unsafe.
As a last resort, I can process the column_type with Pascal's code, but at first, I must make myself sure that the task is not solvable by MySQL's features.
edit:
select column_type from information_schema.columns
where column_name='assettagtype' and table_name='assemblies';
+------------------------------------------+
| COLUMN_TYPE |
+------------------------------------------+
| enum('','И/Н','Н/Н',''''',fgg','''') |
+------------------------------------------+
1 row in set (0.00 sec)
Fourth field: '',fgg, fifth field: '
set #q="'в работе','на списание','списано'";
WITH RECURSIVE cte as (
select 1 as a union all
select a+1 from cte where a<35
)
select distinct regexp_substr(#q,'''[^,]*''',a) as E from cte;
Too high values for 35 raise an error ERROR 3686 (HY000): Index out of bounds in regular expression search.. (I created a bug for this)
The null value should be filtered out... 😉
output:
E
'в работе'
'на списание'
'списано'
null
EDIT: With some effort, this also works for a more complex example (not for every "staged" example!)
set #q="'в работе','на списание','списано',''',fgg'";
select #q;
WITH RECURSIVE cte as (
select 1 as a union all
select a+1 from cte where a<35
)
select distinct regexp_substr(#q,'(''([^,]|[^''][^''])*'')',a) E from cte;
output:
E
'в работе'
'на списание'
'списано'
''',fgg'

Finding a column value with only a line break in MySQL

I have a MySQL DB table where a text column has some values which seem to be only CR and LF control characters (the value is just the line break).
I need a query which will identify all such rows. I tried something like this
SELECT * FROM mytable WHERE mycolumn REGEXP "\r\n";
from here. But that didn't work. I guess I just need the correct regex in my case. Any suggestions would be greatly appreciated.
I inserted a col with line breaks, i could retrieve it with foll SQL
mysql> select lat from TEST_INSERT where lat regexp '.*[\n]';
+--------------+
| lat |
+--------------+
| xx
yy
zz
|
+--------------+
1 row in set (0.00 sec)
WHERE col = "\r\n"
will check for that column having only a Windows-type line break.
WHERE col = "\n"
for unix-style.
Is it what you are looking for???
mysql> select lat from TEST_INSERT where lat regexp '^\n+$';
+-------+
| lat |
+-------+
|
|
+-------+
1 row in set (0.00 sec)

can a mysql select query in C language return a field with the special characters escaped?

1- string = a'b"c\d
2- escaped_string = a\'b\"c\\d
3- make an insert query that inserts escaped_string in some table field.
4- make a select query that returns the inserted value.
The returned value is: a'b"c\d
Is there a way to get the select query to return a\'b\"c\\d ?
(I understand that i can escape it again).
You can use the QUOTE() function of mysql:
mysql> select data from x;
+---------+
| data |
+---------+
| a'b"c\d |
+---------+
1 row in set (0.00 sec)
mysql> select quote(data) from x;
+-------------+
| quote(data) |
+-------------+
| 'a\'b"c\\d' |
+-------------+
1 row in set (0.00 sec)
This should exactly do what you are looking for. Note that the " doesn't need to be escaped here, so QUOTE() doesn't escape it, too.

mysql simple where clause not working?

Database changed
mysql> select * from userinfo;
+-----------+----------+-----------------------+------------+
| firstname | lastname | username | password |
+-----------+----------+-----------------------+------------+
| asif | kolu | ashufound | 123456 |
| faisal | samad | tfhgfhgfh#gmail.com | 123456 |
| kamran | shafat | kamthemaam | kamoos |
| ubaid | mir | sadfsfsff#yahoo.com | qwertasd |
| majid | mir | zsffsa | afdfdsf |
+-----------+----------+-----------------------+------------+
5 rows in set (0.00 sec)
mysql> SELECT * from userinfo WHERE lastname = 'mir';
Empty set (0.10 sec)
mysql> SELECT * from userinfo WHERE lastname='mir';
Empty set (0.00 sec)
what is wrong with this code simple where clause not working?actually problem is in the code for insert i think
your last name in your table may have a space before or after mir
mir
^---^---look and remove spaces from here in your table
I m usign select Query in this the where clause doesnt working query is
select * from table_t where id = '96'
this query is resulting 0 rows but
when i try
select * from table_t where id like '96'
this query is working fine.
and when i try like with column name like
select id from table_t where id like '96'
returning 0 rows
the id is auto generated primary key not have white spaces
why????
is there any database issue???
this query is working fine on my local machine but when i try it online it is misbehaving.
Thanx.
1- You may have space before or after "mir".
2- You may have special (invisible) characters before or after 'mir' or even between its characters.
To solve this problem, I suggest to do this first:
Update userinfo
set lastname = 'mir'
where (username = 'sadfsfsff#yahoo.com') or (username = 'zsffsa')
And then, run this to check:
Select * from userinfo where lastname = 'mir'
I see some good answers but in the case that you couldn't update leading or trailing spaces in your entire db for each name, you could write the select a little differently. If it is a space issue, try this.
SELECT * from userinfo WHERE TRIM(lastname) = TRIM('mir')
If that doesn't work, try LIKE and see if you get results. That could help with debugging.
SELECT * FROM userinfo WHERE lastname LIKE '%mir%'

mysql wildcards % vs %%

What is the difference in '%' and '%%', when used in mysql where clause with 'LIKE' ?
select * from `wp_users` u where u.user_nicename like "%lastuser%"
VS
select * from `wp_users` u where u.user_nicename like "%%lastuser%%"
There is no difference between %% and % when it comes to pattern matching in mysql.
I've seen developers get confused over this when they try to match a literal % and therefor write %%. This is most often because of the fact that format-strings often use a double % to indicate that you'd like it to be treated as an exact literal.
MySQL documentation of LIKE
MySQL 5.0 Reference Manual :: 11.5.1 String Comparison Functions :: LIKE
What's the origin of the string, and where is it going?
If the string is passed to a function such as sprintf the format-string rule I mentioned earlier is present, though there is no confusion in that case.
The developer want it to be a single % in the string passed to mysql, and therefor wrote %%.
$query = sprintf (
"SELECT ... FROM ... WHERE id <> %d AND data LIKE '%%hello world%%'",
50
);
// $query => "SELECT ... FROM ... WHERE id <> 50 AND data LIKE '%hello world%'";
A few sample SELECTs using the LIKE operator
mysql> SELECT 'abc' LIKE 'ab%';
+------------------+
| 'abc' LIKE 'ab%' |
+------------------+
| 1 |
+------------------+
1 row in set (0.01 sec)
mysql> SELECT 'abc' LIKE 'ab%%';
+-------------------+
| 'abc' LIKE 'ab%%' |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT 'abc' LIKE 'ab\%';
+-------------------+
| 'abc' LIKE 'ab\%' |
+-------------------+
| 0 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT 'ab%' LIKE 'ab\%';
+-------------------+
| 'ab%' LIKE 'ab\%' |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)