MySQL using like to match specific string - mysql

In my column I can have either a string like : "data+" or "data+data+data+..(undefined times)..+"
I simply need to get the column where I have multiples data and not only one.
I tried with
mycol NOT LIKE '%+'
But it didn't work...
Actually I don't know the data it is a string that varies : 'blabla' or 'aString' or 'whatever'
If I had in my columns
'jdsjpgsg+jdsjpgsg+', 'dvgff+', 'ffef+eefds+ghghgh+'
I want to select only
'jdsjpgsg+jdsjpgsg+',
'ffef+eefds+ghghgh+',
NOT 'dvgff+' !

if you want to search '..xxx+..' then you should be use xxx+%
if you want to search '..+xxx..' then you should be use %+xxx
if you want to search '..++..' then you should be use %++%
if you want to search '..+..+..' then you should be use %+%+%
It is what I get too and I dont want that. It is actually what i don't want to select. If I had jdsjpgsg+jdsjpgsg+ in my table I want to select it and NOT jdsjpgsg+ It is tricky...
so you can try like '%+%+%' to exclude just one '+'
CREATE TABLE TestTable
(`text` varchar(90))
;
INSERT INTO TestTable
(`text`)
VALUES
('jdsjpgsg+jdsjpgsg+'),
('dvgff+'),
('ffef+eefds+ghghgh+')
;
select * from TestTable
where text like '%+%+%'
| text |
|--------------------|
| jdsjpgsg+jdsjpgsg+ |
| ffef+eefds+ghghgh+ |
SQL Fiddle Demo Link

The % is the wildcard character. You should be using the % after data. Try like:
SELECT * FROM `table` WHERE `mycol` NOT LIKE 'data+%'
The above query will filter out all the records that have any characters after data+.

Related

MySQL where statement based on if

Hi I'm making an MySQL statement from the following logic.
Select * from content
if(description=='educational')
where serial like '%EDU2015%'
else where serial like '%NOVEL2015%'
The logic is simply as, if the description column is educational, the filter should have the string "EDU2015. Else, it should filter with the serial of "NOVEL2015"
I'm not sure if im using the way to use the IF statement in mysql but i dont know where to place the where statements from https://dev.mysql.com/doc/refman/5.7/en/if.html
You can use CASE expression like this:
SELECT * FROM content
WHERE serial like CASE WHEN description = 'educational'
THEN '%EDU2015%'
ELSE '%NOVEL2015%'
END
Note that when you compare values, a single equal sign is required and not two.
EDIT: If you want to filter on different columns based on a condition, you can use this:
SELECT * FROM content
WHERE (description = 'educational' and serial like '%EDU2015%')
OR(description <> 'educational' and id>100)
You can use IF in WHERE block like that:
Select *
from content
where serial like IF(description = 'educational','%EDU2015%','%NOVEL2015%');
Alternatively you can choose CASE WHEN expression like #sagi stated.
mysql> SELECT IF(1>3,'true','false');
+------------------------+
| IF(1>3,'true','false') |
+------------------------+
| false |
+------------------------+
1 row in set (0.00 sec)
This works too
Select * from content
where if(description = 'educational', serial like '%EDU2015%', serial like '%NOVEL2015%');

MYSQL 5.6 : Select where value is in CSV colum

I have a database (Mysql 5.6) like this :
id | value
--------------------------
1 | "value1;value2;value3"
2 | "value4;value5;value6"
I'd like to make a request like :
SELECT id FROM table WHERE 'value5' IS IN value
return --> 2
SQLFiddle : http://sqlfiddle.com/#!9/b5c347
I cannot change the database schema
Use LIKE syntax, but use it like this:
SELECT id
FROM table
WHERE
value LIKE 'value5;%' OR
value LIKE '%;value5' OR
value LIKE '%;value5;%' OR
value LIKE 'value5';
This is a dirty solution, and not possible to index, but should fit your needs (On a smallish table in a reasonable amount of time)
Another solution (If you're sure your values don't contain commas):
SELECT id
FROM table
WHERE
FIND_IN_SET('value5', REPLACE(value, ';', ',')
If all the values are different, you might be able to use LIKE:
SELECT id FROM table WHERE value LIKE '%value5%';
Just keep in mind that it won't work in case you have some value that contains the other, like
value2;value3;value56
Or use a RegExp, like this:
SELECT id FROM table WHERE value REGEXP '(^|[^a-z0-9])value5([^a-z0-9]|$)'

how to search between three different values in mysql?

I have a MySql database with some data like this:
str = '554a41a6e4d9d 677 3'
and i want to search all the values that exactly match the '677' part (the second part of text, after the first).
How i can accomplish this?
If the 'parts' are delimited by space, you can use like:
SELECT [cols] FROM [table] WHERE [col] LIKE '% 677 %'
You should use Fulltext type for the column. And then use a query like this:
SELECT * FROM table WHERE MATCH (column) AGAINST ('677');

MySQL - Need search result of maximum matching letters from a string

Hi I am writing my own MySQL query where I need a result of records as follows.
Word in a table - ABC XYZ
My string - ABC XYZQWER
when I ran my query as below -
SELECT * FROM myTABLE where `column` LIKE 'ABC XYZQWER%';
I am getting empty result. I am aware of the fact that MySQL LIKE matches the result of string.
I need a way to figure this out.
I I searched it using 'ABC X' - it is giving me a proper result.
You can use the function LOCATE():
SELECT `column`
FROM myTable
WHERE LOCATE(`column`, 'ABC XYZQWER') = 1;
As long as there is a value ABC XYZ in the column named column, the result of the query will be at least:
+---------+
| column |
+---------+
| ABC XYZ |
+---------+
Finding an inner match
Finding a matching string like 'BC', which is inside the search string 'ABC XYZQWER', is possible by using the compare operator >=. So the WHERE clause will look like this:
WHERE LOCATE(`column`, 'ABC XYZQWER') >= 1;
It is because you dont have a work which has QWER. You are actually searching for a word which is not present. So you are getting a zero result.
For eg:
Word : qwertyuiuioo
search String : qwerty
select * from table where word like qwerty% you will get the result.
% takes any number of characters after the letters you have given which is not matching any value in the table.
Try this:
SELECT * FROM myTABLE a WHERE 'ABC XYZQWER' LIKE CONCAT(a.column, '%');
Here are some examples of how one might use LIKE clause in SQL queries:
SELECT * FROM myTABLE where column LIKE 'ABC%';// matches ABCD, ABC D but not DABC
SELECT * FROM myTABLE where column LIKE '%ABC%';// matches any string that contains ABC anywhere in the string eg. DABC, D ABC but not D AB C
for your case you would do something like this:
SELECT * FROM myTABLE where column LIKE 'ABC XYZ%';
You won't be able to do perfect substring searches although you can apply Levenshtein distance searches as described here (Levenshtein Distance MySQL Function). But do note these work a bit differently from LIKE clause in a way that it gives you the result based on the distance you specify for a search.
And after that you can use it like this:
SELECT * FROM mytable WHERE levenshtein("ABC XYZQWER",column) <= 4
This will give you the result set you are looking for; it will also give other words which fall under this range.

MySQL select using wildcard (but wildcard in field)

I have a mysql 5 table with a char field holding
DOG
DOUG
CAT
MOUSE
Now I want to SELECT on this field, finding any rows where that field exist within a string, like "DOGGY". (This is opposite of how you normally use a wildcard). So I want a select something like:
SELECT FROM TABLE WHERE FIELD IS SUBSTRING OF "DOGGY"
Is this possible?
select * from mytable where 'doggy' like concat('%',mycol,'%')
You should be able to use LOCATE() to achieve this:
SELECT * FROM MyTable WHERE LOCATE(MyField, 'DOGGY') != 0;