sql query search removing end of string - mysql

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

Related

What is the best way to use MYSQL Search on term not working

What is the best way to search a database for a phrase such as "Almond Anise Cookie" and return the result?
If I
SELECT *
FROM recipes
WHERE name LIKE '%".$query."%'
and use the phrase "Almond Cookie", nothing is returned as expected. But if I search for "Anise Cookie" the result above is returned.
I've also tried
SELECT *
FROM recipes
WHERE name LIKE '%".$query."%'
OR name LIKE '".$query."%'
OR name LIKE '%".$query."'
with the same failed result.
Using MATCH AGAINST returns everything that contains "Almond" and everything that contains "Cookie" also not a good result. Is there a happy middle in returned results?
You can try using REPLACE. Something like this should work:
SELECT *
FROM recipes
WHERE NAME LIKE REPLACE(' ".$query." ',' ','%');
Note that I purposely add spaces between .$query. to ensure that the replace operation will make your term filled with the wildcard symbol. In the example above:
If $query='almond cookies' then REPLACE(' ".$query." ',' ','%') will become %almond%cookies%.
You can test the fiddle here : https://www.db-fiddle.com/f/kMzp99S8ENbTkYcW5FVdYN/0

msyql searching a domain, without the TLD extension

How can we search for a domain, without the TLD in mysql, so for e.g. testdomain.com, I would want to search only testdomain not the .com, so a search for test would return row, but a search for com would not.
I assume it would be similar to below with some regex, but no idea how to achieve that.
SELECT * FROM domains WHERE domain_name LIKE '%$search%'
Any idea on how to to search just that part of the domain?
You can do something like:
SELECT * FROM domains
WHERE SUBSTRING_INDEX(domain_name, '.', 1) LIKE '%$search%'
if you are looking for search a name starting with a string your query must be:
SELECT * FROM domains WHERE domain_name LIKE '$search%'
this query is a good query because it use indexes.
adding the "." character at the end you will find only the full name,
also this query is a good query because it use indexes.
SELECT * FROM domains WHERE domain_name LIKE '$search.%'
Else if you want to make a partial search you need to add the % before and after the term but in this case the "com" search will match, this search is not good becouse it do not use indexes.
At last this expressions search for a string containing the name excluding the TLD, this is not a good query because it do not use indexes.
SELECT * FROM domains WHERE domain_name LIKE '%$search%' and not like '%.$search%'
A good idea could be to split fields in your database, make a column (or an additional colunm) for the domain name without TLD and search in this new coloumn.

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

Matching websites in MySQL via regexp

I have two columns: "website 1" and "website 2" and I want to test for equality. The issue is that there are differences in format such as:
www.example.com http://www.example.com/
example2.co.uk/ https://www.example2.co.uk/Default.aspx
I would like to be able to match the domain only. Can this be done in MySQL given a regexp such as:
(http://|https://)?(www.)?[A-z]*(.com|.co.uk|.us|.org|.net|.mobi)
This can be done without RegEx,
TRY something like this to only take out the domain
SELECT
(t1.website1, instr(reverse(t1.website1),'.')),
(t1.website2, instr(reverse(t1.website2),'.'))
FROM table1 t1
Then you can check for equality on the domain.
You may also have to replace also the data from / and to end with nothing in your select. Otherwise .aspx will be a domin in above case for instance.
You will also have to have a extra column that counts the amount of . , since some domains have 2 like .co.uk / .co.jp
like this
SELECT LEN('t1.website1') - LEN(REPLACE('t1.website1','.','')) AS AmountOfDotsInString
This gives you a count on how many dots exists in the string, do this count after the data after any / has been cleared.

MySQL regex only returns a single row

I have been writing a REGEX in MySQL to identify those domains that have a .com TLD. The URLs are usually of the form
http://example.com/
The regex I came up with looks like this:
REGEXP '[[.colon.]][[.slash.]][[.slash.]]([:alnum:]+)[[...]]com[[./.]]'
The reason we match the :// is so that we don't pick up URLs such as http://example.com/error.com/wrong.com
Therefore my query is
SELECT DISTINCT name
FROM table
WHERE name REGEXP '[[.colon.]][[.slash.]][[.slash.]]([:alnum:]+)[[...]]com[[./.]]'"
However, this is returning only a single row when it should really be returning many more (upwards of a thousand). What mistake am I making with the query?
Not sure if that's the problem, but it should be [[:alnum:]], not [:alnum:]
Your current query only matches names that end with .com/ rather than .com followed by anything that starts with a slash. Try the following:
SELECT DISTINCT name
FROM table
WHERE name REGEXP '[[.colon.]][[.slash.]][[.slash.]]([:alnum:]+)[[...]]com([[./.]].*)?'"
It might be clearer to split the URL rather than regexing it
SELECT DISTINCT name FROM table
WHERE SUBSTRING_INDEX((SUBSTRING_INDEX(name,'/',3),'.',-1)='com';