Sql select url regex - mysql

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

Related

How to escape slashes in LIKE clause

I need to do some clean-up in a database and change some urls from http://www.domain1.com to http://www.domain2.com, so I am doing a query like this:
UPDATE mytable set url = REPLACE(url, '"http://www.domain1.com"', '"http://www.domain2.com"')
the double comma is needed as this strings are part of a JSON string
however, the problem is that in the DB the old url is for some reason escaped so I have something like this
http:\/\/www.domain1.com
if I run a query like these they don't match anything
select * from mytable where url like '%http://www.domain1.com%'
select * from mytable where url like '%http:\/\/www.domain1.com%'
and therefore the replace query doesn't work either, so what's the correct way to escape these strings?
You might try:
where url like '%http:\\/\\/www.domain1.com%'
To be honest, I don't understand how or why forward slashes would be escaped. Perhaps you are better off just using a wildcard:
where url like '%http:_/_/www.domain1.com%'

sql query search removing end of string

I am trying to search domain names ending in particular keywords. e.g. "car" would bring up buycar.com, but not carbuy.com.
So if my query is
SELECT * FROM domains WHERE LIKE '%car'
Will not show any results at all, obviously because the domains dont end in car, they end in .com, or .co, or something.
I think I need to do some sort of regex replace to search the domain, until the first .
Or whatever would do the equivalent of this in sql for php:
$pos = strpos($domain,'.');
$search = substr($domain,0,$pos);
So it would just search the actual domain without the TLD. Is this possible with sql?
How about using:
SELECT *
FROM domains
WHERE domain LIKE '%car.%'
You could remove characters like .com, .IR, .co after car and run own query.
Please try this:
SELECT *
FROM domains
WHERE
REVERSE(SUBSTRING(REVERSE(domain),CHARINDEX('.',REVERSE(domain))+1,LEN(domain
))) LIKE '%car'
if you don't remove that character and use Like '%car.%' maybe get some thing like this: car.site.com

More efficient than using lots of LIKE queries 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...

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

Return records that contain specific string but from the 7th character

I have a mysql field that has text such as "http://www.example.com/hello.php" in the "url" column.
I need some sort of mysql query that will allow me to find urls that have two or more slashes. Of course using '%//%' will return all of my urls due to the http:// aspect of the URLs.
So how can I get mysql to only look at urls from the 7th character?
using underscore _ wildcard character to represent the first 7 characters (http://) followed by the pattern you are searching for e.g:
select * from urls where url like '_______%//%'; -- double //
or
select * from urls where url like '_______%/%'; -- single /
or
select * from urls where url like '_______%google%';
Regular expressions ? http://dev.mysql.com/doc/refman/5.1/en/regexp.html - you can do more with this
This should work:
SELECT * FROM table WHERE url LIKE 'http://%//%'
Using regular expressions would be a good way to go.
select * from urls where url regexp '[/].+[/].+[/]';