I'm trying to delete lines in specific column from all rows that contains specific words.
For example:
Remove lines that contain word apple and it is always at the beginning of the line.
+--+------------------+
|ID|data |
+--+------------------+
|1 |sometext1 |
| |sometext2 |
| |apple sometext3 |
| |sometext4 |
+--+------------------+
|2 |apple sometext5 |
| |sometext6 |
+--+------------------+
so the result would be:
+--+------------------+
|ID|data |
+--+------------------+
|1 |sometext1 |
| |sometext2 |
| |sometext4 |
+--+------------------+
|2 |sometext6 |
+--+------------------+
'SometextX' is different in every line, number of lines is different in every row and it has different number of characters in every line.
I really need this in MySQL any help would be appreciated.
You would be better off using REGEXP here to match patterns in each line:
DELETE FROM yourTable WHERE text REGEXP '^apple';
REGEXP allows for fairly complex regex matching, and would be useful if your requirement changes or gets more complex later on.
Edit: MySQL has no built in support for regex replacement, so there is no easy way to accomplish what you want.
A general regex pattern to remove the word apple would be \bapple\b. You may search on this pattern and replace with empty string.
You would use where:
where textcol not like 'apple%' or textcol is null
This can be part of a select or a delete (the question mentions "result" which suggests the former and "delete" which suggests the latter). It is not clear whether you actually want to change the data or whether you just want the result set without these words.
Note: you can do this without or and still handle NULL values, because MySQL has a NULL-safe equality operator:
where not left(textcol, 5) <=> 'apple'
You can use MySQL functions to select the right rows and to update with new data as follows:
UPDATE `yourTable` SET `yourField` = REPLACE(yourField, 'apple', '') WHERE yourField LIKE '%apple%'
If you don't want to delete the whole row, you can run these 3 queries in this order
update your_table set text=replace(text,substring(text,#start:=locate('\napple',text),locate('\n',text,#start+1)-#start+1),'');
update your_table set text=if((#start:=locate('apple',text))=1,replace(text,substring(text,#start,locate('\n',text,#start+1)-#start+1),''),text);
update your_table set text=if((#start:=locate('apple',text))=1,replace(text,substring(text,locate('apple',text)),''),text);
update #1 will remove apple in the middle of the text (prefixed by \n)
update #2 will remove apple at the beginning of its row (nothing before) and having following rows
update #3 will remove remaining cases
Related
I am trying to remove part of a string. But can't do it properly. String is like this: 4,290TL〜9,490TL So trying to remove after this 〜
I tried
UPDATE SET price = SUBSTRING_INDEX(price, '〜')
But not worked.
SUBSTRiNG_INDEX requires 3 parameters, the last one being the delimiter count. In this case you need to supply a count of 1, indicating that you want everything to the left of the first occurrence of the delimiter 〜. Additionally, you need to specify your table name in the query. Try this:
UPDATE yourtable SET price = SUBSTRING_INDEX(price, '〜', 1)
UPDATE SET price = SUBSTRING_INDEX(price, '〜', 1)
Please note the the strings you have shared with this question or comments uses DIFFERENT wavy line ("tilde") characters
# the tilde character used here is DIFFERENT to the next example
select substring_index('4,290TL〜9,490TL','〜',1)
;
+----+------------------------------------------------------+
| | substring_index('4,290TL〜9,490TL','〜',1) |
+----+------------------------------------------------------+
| 1 | 4,290TL |
# the tilde character is different to the one above
select substring_index('18,990万円(1戸)~28,790万円(1戸)','~',1)
;
+----+-----------------------------------------------------------+
| | substring_index('18,990万円(1戸)~28,790万円(1戸)','~',1) |
+----+-----------------------------------------------------------+
| 1 | 18,990万円(1戸) |
You will need to be CERTAIN the the tilde you use as delimiter is the one you use in substring_index() otherwise that function will just return the whole string.
Is there a built in way in mysql to query columns that contains specific word(s) and the word(s) also start with the input.
For instance, input is "EF"
#1 ABCD EFGH <- Want this one
#2 BCDE FGHA
#3 CDEF GHAB
#4 EFGH ABCD <- And this one
If I query %EF%, it'll give me the #1, #3 and #4, if I query EF%, it's only going to give #4.
So essentially, I want to know if mysql can firstly do a preg_split like in PHP, then query EF%, or something along the line of that.
Use a regular expression. In MySQL, the regexp pattern [[:<:]] matches the beginning of a word, so you can do:
WHERE column REGEXP '[[:<:]]EF'
Documentation
Answering my own question here. Just came up with a solution, not pretty but it works.
I just changed my parameter into this
((Column LIKE '%...%' AND Column LIKE '% ...%') Or (Column LIKE '...%')) AND ...
Going to leave this here for a day and see if anyone has a better solution.
If your column has two words, then you can use SUBSTR() and LOCATE().
+--------------------------------+
| MyColumn |
+--------------------------------+
| Apple Effect |
| Effortless Orange |
+--------------------------------+
.
SELECT *
FROM (
SELECT
*,
SUBSTR(#values,1,LOCATE(' ',MyColumn)-1) AS FirstWord
SUBSTR(#values,LOCATE(' ',#values)+1) AS SecondWord
FROM MyTable
) AS X
WHERE FirstWord LIKE 'EF%'
OR SecondWord LIKE 'EF%'
If your column has more than two words, then things are a little more complex, but you can use SUBSTRING_INDEX() to find the Nth instance.
+--------------------------------+
| MyColumn |
+--------------------------------+
| The Perfect Apple Effect |
| The Special Effortless Orange |
+--------------------------------+
.
SUBSTRING_INDEX(SUBSTRING_INDEX(#values,' ',4),' ',-1) = Gets the fourth word.
More examples and information can be found here.
Been struggling for this for awhile.
Is there a way to find all rows in my table where the word in the column 'word' is a part of a search word?
+---------+-----------------+
| id_word | word |
+---------+-----------------+
| 177041 | utvälj |
| 119270 | fonders |
| 39968 | flamländarens |
| 63567 | hänvisningarnas |
| 61244 | hovdansers |
+---------+-----------------+
I want to extract the row 119270, fonders. I want to do this by passing in the word 'plafonders'.
SELECT * FROM words WHERE word REGEXP 'plafonders$'
That query will of course not work in this case, would've been perfect if it had been the other way around.
Does anyone know a solution to this?
SELECT * FROM words WHERE 'plafonders' REGEXP concat(word, '$')
should accomplish what you want. Your regex:
plafonders$
is looking for plafonders at the end of the column. This is looking for everything the column has until its end, e.g. the regexp is fonders$ for 119270.
See https://regex101.com/r/Ytb3kg/1/ compared to https://regex101.com/r/Ytb3kg/2/.
MySQL's REGEXP does not handle accented letters very well. Perhaps it will work OK in your limited situation.
Here's a slightly faster approach (though it still requires a table scan):
SELECT * FROM words
WHERE 'PLAutvälj' =
RIGHT('PLAutvälj', CHAR_LENGTH(word)) = word;
(To check the accents, I picked a different word from your table.)
I have a mysql table with a column that looks something like this:
| TAGS |
------
|Green |
|Blue |
|Orange|
|Blue |
|Green |
| ... |
------
Now what I want to do is output all the different tags that exist in a list, BUT every tag can only be outputted once (so e.g. 'Green stands two times in the database but can only stands one time in the list')
Hope you understand my question!
Thanks
You said "row" but do you mean a column labeled "tags" in your table?
There are two different ways:
Method 1:
SELECT DISTINCT tags FROM table_name WHERE condition;
Method 2:
SELECT tags FROM table_name WHERE condition GROUP BY tags;
They both will return a you an array where each item in the row is the tag without repeats (distinct). The main difference is that DISTINCT makes it easier to optimize (and possibly quicker).
Now, if you didn't make a typo and said that you have a row with a column that has multiple tags like:
|ROW_ID|TAGS |
| 1 |'Blue', 'Red' |
| 2 |'Red', 'Yellow' |
| 3 |'Blue', 'Black', 'Red'|
Then you'll have to do some parsing and array operations (but that's a completely different answer).
I am using MySQL. In one of my table attributes, I have a serial number description like "SM,ST,SK" for one device.
When users enter SM or ST or SK, I want my query to return a result
My current query looks like that:
SELECT CONCAT(lvl1_id,',',lvl2_id)
FROM hier_menus
LEFT JOIN labels ON (hier_menus.id=label_id AND tbl=65 AND fld=2 AND lang_id=5)
WHERE
hm_type=13 AND lvl1_id=141 AND lvl2_id=id AND label='".addslashes($serial)."'";
It is only able to look at the first comma part of serial number column. When users enter ST, it will not return anything.
Is it possible to search the whole of the long string "SM,ST,SK" to return a matching row?
mysql> select find_in_set('SK', 'SM,ST,SK');
+-------------------------------+
| find_in_set('SK', 'SM,ST,SK') |
+-------------------------------+
| 3 |
+-------------------------------+
1 row in set (0.00 sec)
mysql> select find_in_set('SP', 'SM,ST,SK');
+-------------------------------+
| find_in_set('SP', 'SM,ST,SK') |
+-------------------------------+
| 0 |
+-------------------------------+
You are looking for find_in_set,
however, this is not an optimize solution
you should seek to normalize your serial number into another table,
where each SM,ST, and SK is stored as one row
another way is to convert the data type to set
Try FIND_IN_SET():
SELECT ... WHERE FIND_IN_SET($serial, label)
and as ajreal's pointed out, don't use addslashes. use mysql_real_escape_string (or whatever your DB abstraction library provides). addslashes is hopelessly broken and WILL allow someone to attack your database with ease.