More efficient than using lots of LIKE queries mysql - mysql

I need to query a database to find certain urls containing a certian set of criteria for example : "MY" AND "sand" in any order.
I am currently using LIKE '%MY%' AND LIKE '%Sand%' is there a btter way of executing this?
Thanks

Could try REGEXP e.g:
WHERE url REGEXP '(my.*sand|sand.*my)'
Or alternatively:
WHERE URL REGEXP 'my' AND url REGEXP 'sand'
Not sure how the speed will compare...

Related

How to extract id from url in a query?

I have a table with a column containing many urls like this one:
https://myshop.com/lv/buitine-technika-elektronika/virtuves-iranga/virduliai/virdulys-elektrinis-virdulys-forme-fkg-147?id=22031685
I want to extract the id from the URL, but I have no idea how to do this. I could easily do this in Python later using this regex:
\?id\=(\d+)
But I'd like to have as much of data prepared in my query before going to python if that is possible. I know how to use regex in MySQL where clause, but no idea how to use it anywhere else. Is there a way to do this?
ID length can be different and there might be something else after that...
If every URL would only ever have a single id query parameter, then we can use SUBSTRING_INDEX:
SELECT SUBSTRING_INDEX(url, '?id=', -1) AS id
FROM yourTable;
Demo
For a more general solution in MySQL 8+, we can use REGEXP_SUBSTRING:
SELECT REGEXP_REPLACE(url, '^.*?.*id=([^&]+).*$', '$1') AS id
FROM yourTable;
Demo
The regex based approach can handle id appearing anywhere in the query string.

How to make mysql LIKE search for exactly single words only

I have a query like this:
SELECT * FROM mytable where description like %STRING%
The problem is: When I search for JAVAit returns me even the records with JAVAscript.
But, JAVA != JavaScript, right ? How can I work around it ?
MySQL's LIKE operator isn't really suitable to detect an exact single word inside a string. But REGEXP, which supports regular expressions, can handle this. Consider the following query:
SELECT * FROM mytable WHERE description REGEXP '[[:<:]]Java[[:>:]]';
This corresponds to matching the pattern \bJava\b, i.e. the word Java by itself.
Demo
Edit:
If you are trying to execute this query using Laravel, then whereRaw should come in handy:
$results = DB::table('mytable')
->whereRaw('description REGEXP ?', ['[[:<:]]Java[[:>:]]'])
->get();

Sql select url regex

So I have to find the urls which contains the word 'foo' for example . I want to do this with a regex like SELECT * FROM table WHERE url = regex. The url is https//... OR http://.... and I need to find the word foo for example in the url. What I actually want is a regex which will check if foo is in the hostname or in the path. Thanks for help!
EDIT :
I want to find a faster alternative with regex because LIKE '%foo%' is very slow on my table.
Use LIKE with the % wildcard operator.
SELECT * FROM table WHERE url LIKE '%foo%'

(mySql) query to match /exact_string/[wild card]/exact_string/[wild card] etc?

My URLs look like
'/api/comments/languages/124/component/segment_translation/2'
I've know which parts of the url are static; and which are dynamic - and have a structure which tells me this
I have example requests and responses (where the dynamic parts won't match) - which I'm trying to look up in mySQL - so I could very easily generate a query
select url from qb_log_full_requests
where
URL REGEXP 'api/comments/languages/[^f.*]/component/[^f.*]/[^f.*]'
Which is great; except it doesn't work.
Is there a way to ask mySQL to match
/exact_string/[wild card]/exact_string/[wild card]
etc?
You may try following regexp:
/api/comments/languages/[^/]+/component/segment_translation/[^/]+

Simple MySQL query not selecting all data?

I know a bit about MySQL, but not enough to know why this query is not working.
SELECT * FROM files WHERE uploader LIKE 'value';
I have a database of files which contains uploader names. Yet, when I search for an uploader name it misses a lot of the entries, even though the name is completely identical all through.
No idea why it does this.
Thanks in advance.
Sonny, you need some wildcards in that there LIKE clause.
SELECT * FROM files WHERE uploader LIKE '%value%';
If you are searching for an identical uploader name, for example, an uploader named Jason, then use the equals comparison operator.
SELECT * FROM files WHERE uploader = 'Jason';
If you would like to match ssJasonsss, Jason, etc, then use:
SELECT * FROM files WHERE uploader LIKE '%Jason%';
Try below :
SELECT * FROM files WHERE uploader LIKE '%value%'
read more details about Pattern Matching
When you use like you usually want to include some sort of wildcard:
SELECT * FROM files WHERE uploader LIKE '%value%';
For undefined number of characters use % for just one additional character use _.
Some examples:
'%value%' will match:
somevaluesome
somevalue
valuesome
value
Another sample:
'_value_' will match:
avaluea
but not
value
avalue
valuea
You can find more wildcards here:
http://dev.mysql.com/doc/refman/5.1/en/string-comparison-functions.html