MYSQL Like Improper results - mysql

I am using Like to find something pattern from table
sample table:
id | title |
=============
1 | f550 |
-------------
2 | f550 |
-------------
3 | f-550 |
-------------
4 | f 550 |
I am using LIke query to check my receords, so lets say if I search for f550 it is bringing only 2 records that is correct technically but I want all records having any pattern such as (f550,f-550,f 550)
Is there any way besides REGEX I can do?
My query fires like this
SELECT * FROM `qd_posts` WHERE title LIKE '%f550%';
I tried using different combinations such like this but didn't worked.
SELECT * FROM `qd_posts` WHERE title LIKE '%f%-%550%';
I have even tried using RLIKE but still not got the result

You could try using '%f%550%'
SELECT *
FROM `qd_posts`
WHERE title LIKE '%f%550%';
or
SELECT *
FROM `qd_posts`
WHERE title LIKE 'f%' AND LIKE '%550';

For the specific list given (f550,f-550,f 550), use this:
REGEXP '^f[- ]?550$'
Yes, LIKE 'f%550' matches the same things, but lots more.

Related

MySQL query - how to look for certain string in the field

I have a table "story" as follows:
+++++++++++++++++++++++++++++++++++++++++++
| id | keywords |
+++++++++++++++++++++++++++++++++++++++++++
| 1 | romance,movie,drama |
| 2 | newmovie,horor,comedy |
| 3 | movie,scifi |
| 4 | newmovie,romance,drama,asia |
| 5 | kids,movie |
+++++++++++++++++++++++++++++++++++++++++++
I try a query to search 'movie' in keywords field as below:
SELECT id FROM story WHERE keywords LIKE '%movie%'
and the result is
1,2,3,4,5
but in this case I wanted the result is 1,3,5 (field value with newmovie not include). Can someone help me how the query to do it?
Thank you for your help..
You want to use find_in_set like this:
SELECT id FROM story WHERE find_in_set('movie', keywords) > 0;
Though you should really consider normalizing your table structure.
In this case, you could've stored one single keyword in one row, then the query would be simply like:
select id from story where keyword = 'movie';
and that would've been the end of it. No heavy string functions needed.
You could have structure like this:
keywords(id, name);
story(story_id,. . ., keyword_id);
then, you could simply join the two like this:
select s.*
from story s
inner join keywords k on s.keyword_id = k.id
where k.name = 'movie';
Your problem is that "newmovie" can be found by "%movie%" you need only search "movie".

MySQL. Way to compose query with "LIKE" which will work similar as url regular expression

for example I have table 'urls'
urls:
___________________________________________
| id | href |
+--------+--------------------------------|
| 1 | /a/b/c/d/e/f/g/ |
+--------+--------------------------------|
| 2 | /a/b/g/ |
+--------+--------------------------------|
| 3 | /a/c/g/ |
+--------+--------------------------------|
| 4 | /a/d/g/ |
+--------+--------------------------------|
| 5 | /a.php?code=g |
+--------+--------------------------------|
| N | anyUrlString |
+--------+--------------------------------|
I wanna select urls which have special format, for example (like ROUTE in popular PHP frameworks)
"/a/#anyparam/g"
so: WHERE href LIKE '/a/%/g'
but it also will select row with id 5, 1..
How to compose LIKE statement to I can get only URI enabled values in #anyparam ?
must be something like this /a/[%, but not ('/','?','\')]/g but what exactly?
Thanks for any proposition!
P.S. Do not propose to use regular expression (it don't use indexes)!
Can you use multiple clauses?
LIKE '/a/%/g' AND NOT LIKE '/a/%?%/g' ....
Chain some "exceptions" together, attacking cases that do not match. It's hard to come up with a general case, with your limited sample set. An EXPLAIN will show if Indexes are still in use.
String and pattern taken from your code:
SELECT 1 WHERE '/a.php?code=g' LIKE '/a/%/g'
^ should not give you anything, since the string your testing doesn't end in /g, it ends with =g.
But an often overlooked ability of LIKE is negative character-classes. Unfortunately they are not variable-length, they always only represent 1 character, but you could REPEAT() them:
LIKE '/a/' + REPEAT('[^/?\]', LEN(#someVar)) + '/g'

MySQL matching partial strings with LIKE

I'm trying create a MySQL query which will return a row (or more, but I'm only interested in a single row) an area suggestion when provided with a postcode.
So, I have a table like so
area_id | postcode_start
1 | NE
2 | DL
3 | DL1
4 | DL2
5 | DL6
...
And I wish to provide a postcode, for example DL5 8TB and for it to return the row 1 | DL as the first characters match.
Another example DL6 4GH would return two rows, 5 | DL6 and 2 | DL
SELECT *
FROM (`area_postcodes`)
WHERE CONCAT(LOWER(`area_postcodes`.`postcode_start`),'%')
LIKE 'dl5 8tb';
Apparently it's a valid query but it always returns an empty set.
You'll see I've added the wildcard % to the other side of the query but it doesn't seem to work and I don't know where else to even consider guessing how to go about it, it's almost like the reverse of a normal MySQL LIKE query.
You were very close:
SELECT
*
FROM
area_postcodes
WHERE
'dl5 8tb' LIKE CONCAT(postcode_start, '%');

SELECTing (almost) duplicate rows

My table looks something like this:
| id (int) | sentence (varchar) |
I want to find all rows that are almost the same except for one particular word. Eg:
| 230 | test |
| 321 | test sth |
...
| 329 | is (sth) it?
| 923 | is it?
The word that can be different is sth in this case. Ideally I could use some sort of "array" with the list of words that can be different.
Is this something I could do purely in SQL?
Just an untested quick shot, sorry, but I think you could do something like
SELECT * FROM table GROUP BY REPLACE(text, 'sth', '')
You can use SOUNDEX. So with the examples that you gave, these queries:
SELECT SOUNDEX('test')
SELECT SOUNDEX('test sth')
SELECT SOUNDEX('is (sth) it?')
SELECT SOUNDEX('is it?')
return these results:
T230
T230
I200
I200
That means that the first two and the second two sound like each other. What I can't be sure of is how well this will work with your actual data, you're just going to have to try it.

Distinct values in set

the problem is I don't know how to write a query that does what I want. I hope you can help. I have a table like this:
path (VARCHAR) | info (VARCHAR) | method (SET)
------------------------------------------------------------
/ | something | GET
/cp/product/% | something else | GET,PUT
/cp/product/edit/% | yap yap | DELETE,POST
What I want is a query which would show all methods available for the path, e.g. if I were to run something like this:
SELECT distinct_values_in_set(`method`) FROM `table` WHERE '/cp/product/edit/1337' LIKE `vpath`;
I'd expect a result set similar to
distinct_values_in_set(`method`)
--------------------------------
DELETE,GET,POST,PUT
SELECT group_concat(distinct `method`)
FROM `table`
WHERE ...;