MySQL where statement based on if - mysql

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%');

Related

MySQL using like to match specific string

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+.

correct method to Use sub string in where clause mysql query

I have a table in mysql with column called type. contents are below
Type
Test 12
Test abc
start 1
start abcd
end 123
Now I want to select records where type starts with Test
Expected result:
Test 12
Test abc
But I am getting either
Test 12
or empty results
I have tried like below:
select * from table where type = 'Test 12'
select * from table where type = '%Test%'
select * from table where type = '%Test'
What should be the correct sql statement.
If you want to do partial matches you need the LIKE operator:
SELECT * FROM table WHERE type LIKE 'Test%'
Use the Like keyword
select * from table where type LIKE 'Test%'
The equality (=) operator doesn't accept wild cards. You should use the like operator instead:
SELECT * FROM table WHERE type LIKE 'Test%'
Sooo close! You want to start with the string, then don't use % in the start, and use LIKE instead of =
select * from table where type LIKE 'Test%'

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]|$)'

Query to check if a certain row has 2 words

How to return rows where a column has 2 words (that is, strings separated by a space) in it?
It must be purely using SQL.
SELECT * FROM table WHERE name (has 2 strings in it);
I dont know the names when querying. Its a big dataset. I only have to check if the name contains a spacebar basically (from a comment).
If you want to distinguish names that have two parts from one-part and three-plus-part names, you can use regular expression:
SELECT * FROM my_table WHERE name REGEXP '^[^ ]+[ ]+[^ ]+$'
This regular expression matches when the entire string consists of two non-empty parts containing no spaces, with one or more space separating them.
This perfectly works for me
You can use 'AND' condition and Like Operator with wildcards (%).
SELECT * FROM table_name WHERE name LIKE '%Word1%' AND name LIKE '%Word2%'
How about simply:
...
WHERE [name] LIKE '%Word1%'
AND [name] LIKE '%Word2%'
SELECT * FROM table WHERE concat(' ',name,' ') like '% str1 %'
AND concat(' ',name,' ') like '% str2 %'
The extra blanks are there to separate words.
You can use the following technique
mysql> select length('first name')-length(replace('first name',' ','')) as diff;
+------+
| diff |
+------+
| 1 |
+------+
i.e. get the difference of actual name and the name after replacing space, and if its 1 then you have the value as firstname lastname
So the query may look like
select * from table
where
length(col_name)-length(replace(col_name,' ','')) = 1
Use % between words.
Example:
SELECT * FROM Table WHERE Col LIKE '%word1%word2%'
SELECT * FROM table_name WHERE name LIKE '% %'

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.