whole phrase priority searching - mysql

we are implementing a search application
we have implemented a exact word search by the following sql query
SELECT *
FROM jreviews_content
WHERE jr_produits REGEXP '[[:<:]]ryan[[:>:]]'
which works well now we have another requirement
If the user enter a word "Cabaret Mile-End" various result comes up which has Cabaret and Mile-End in it but the row that has whole phrase in it comes at the lastResult.So i want a whole phrase priority wise searching.

As my comment said:
Try using the LIKE structure as follows
SELECT *
FROM jreviews_content
WHERE jr_produits LIKE '%Cabaret Mile-End%'
UNION ALL
SELECT *
FROM jreviews_content
WHERE jr_produits REGEXP '[[:<:]]Cabaret Mile-End[[:>:]]'
I dont know what you put in the last query so I put the value just in between.

Related

MySQL - need to find records without a period in them

I've been to the regexp page on the MySQL website and am having trouble getting the query right. I have a list of links and I want to find invalid links that do not contain a period. Here's my code that doesn't work:
select * from `links` where (url REGEXP '[^\\.]')
It's returning all rows in the entire database. I just want it to show me the rows where 'url' doesn't contain a period. Thanks for your help!
SELECT c1 FROM t1 WHERE c1 NOT LIKE '%.%'
Your regexp matches anything that contains a character that isn't a period. So if it contains foo.bar, the regexp matches the f and succeeds. You can do:
WHERE url REGEXP '^[^.]*$'
The anchors and repetition operator make this check that every character is not a period. Or you can do:
WHERE LOCATE(url, '.') = 0
BTW, you don't need to escape . when it's inside [] in a regexp.
Using regexp seems like an overkill here. A simple like operator would do the trick:
SELECT * FROM `links` WHERE url NOT LIKE '%.%
EDIT:
Having said that, if you really want to negate regexp, just use not regexp:
SELECT * FROM `links` WHERE url NOT REGEXP '[\\.]';

Query MySQL field for LIKE string

I have a field called 'areasCovered' in a MySQL database, which contains a string list of postcodes.
There are 2 rows that have similar data e.g:
Row 1: 'B*,PO*,WA*'
Row 2: 'BB*, SO*, DE*'
Note - The strings are not in any particular order and could change depending on the user
Now, if I was to use a query like:
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%'
I'd like it to return JUST Row 1. However, it's returning Row 2 aswell, because of the BB* in the string.
How could I prevent it from doing this?
The key to using like in this case is to include delimiters, so you can look for delimited values:
SELECT *
FROM technicians
WHERE concat(', ', areasCovered, ', ') LIKE '%, B*, %'
In MySQL, you can also use find_in_set(), but the space can cause you problems so you need to get rid of it:
SELECT *
FROM technicians
WHERE find_in_set('B', replace(areasCovered, ', ', ',') > 0
Finally, though, you should not be storing these types of lists as strings. You should be storing them in a separate table, a junction table, with one row per technician and per area covered. That makes these types of queries easier to express and they have better performance.
You are searching wild cards at the start as well as end.
You need only at end.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Reference:
Normally I hate REGEXP. But ho hum:
SELECT * FROM technicians
WHERE concat(",",replace(areasCovered,", ",",")) regexp ',B{1}\\*';
To explain a bit:
Get rid of the pesky space:
select replace("B*,PO*,WA*",", ",",");
Bolt a comma on the front
select concat(",",replace("B*,PO*,WA*",", ",","));
Use a REGEX to match "comma B once followed by an asterix":
select concat(",",replace("B*,PO*,WA*",", ",",")) regexp ',B{1}\\*';
I could not check it on my machine, but it's should work:
SELECT * FROM technicians WHERE areasCovered <> replace(areaCovered,',B*','whatever')
In case the 'B*' does not exist, the areasCovered will be equal to replace(areaCovered,',B*','whatever'), and it will reject that row.
In case the 'B*' exists, the areCovered will NOT be eqaul to replace(areaCovered,',B*','whatever'), and it will accept that row.
You can Do it the way Programming Student suggested
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Or you can also use limit on query
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%' LIMIT 1
%B*% contains % on each side which makes it to return all the rows where value contains B* at any position of the text however your requirement is to find all the rows which contains values starting with B* so following query should do the work.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'

MySQL LIke statement - multiple words

What would be the right SQL statement so that when I search two words, like for example 'text field' in a text box, it will return all results that has 'text' and 'field' in it using the LIKE statement? I cant find the right terms to make a search. If possible, I want to make it dynamic. Like if a user search 5 words, all 5 words would be in the Like statement. I am trying to achieve a statement something like this.
SELECT *
FROM TABLE
WHERE SEARCH (LIKE %searchterm1%)
OR (LIKE %searchterm2%)
OR (LIKE %searchterm3%) ....
Try This. http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
SELECT *
FROM TABLE
WHERE SEARCH
REGEXP 'searchterm1|searchterm2|searchterm3'
Here's an example of a SQL SELECT statement that uses the LIKE comparison operator
SELECT t.*
FROM mytable t
WHERE t.col LIKE CONCAT('%','cdef','%')
AND t.col LIKE CONCAT('%','hijk','%')
AND t.col LIKE CONCAT('%','mnop','%')
Only rows that have a value in the col column that contains all of the strings 'cdef', 'hijk', and 'mnop' will be returned.
You specifically asked about the LIKE comparison operator. There's also a REGEXP operator that matches regular expressions. And the Full-Text search feature may be a good fit your use case.

Simple select query not working?

PROVINCE -- varchar(25)
SELECT * FROM listings WHERE PROVINCE='Bakersfield'
Returns empty resultset when entries with Bakersfield as province exist
I am pretty unsure as to why. When I remove the WHERE it works.
BTW I am running this in phpmyadmin, where it lets one execute SQL statements
In phpmyadmin it shows ... beneath the equals sign. I am not sure why. That may be a hint.
If
SELECT * FROM listings WHERE PROVINCE like '%Bakersfield%'
works for you then you have spaces in your data. You can remove them with
update listings
set PROVINCE = trim(PROVINCE)
See TRIM()
After that you should check your code where you insert the data. Check why there are spaces inserted and fix it.
Change you equal = to LIKE operator
SELECT * FROM listings WHERE PROVINCE LIKE 'Bakersfield'
This should solve your issue. If you want to find places that includes your string simply surround it with wildcards %..%
SELECT * FROM listings WHERE PROVINCE LIKE '%Bakersfield%'
SELECT * FROM listings WHERE PROVINCE='Bakersfield'
There is no point the abpve query doesnt work if the follwing case is not the problem.
1) If case is the prob then use upper or lower
SELECT * FROM listings WHERE upper(PROVINCE)=upper('Bakersfield');
2) If extra space is the problem use replace or Trim
SELECT * FROM listings WHERE replace(PROVINCE,' ','') ='Bakersfield'
There might be sum other seniario if not then use "Like " key word as suggested by others. :)

mySQL WHERE.. OR query

I want to execute a query on a database to select all rows in the 'Event' table where the 'about' section has any of the following words in it: strokestown, arts, day. My query, shown below is only getting rows that have the first word, strokestown in them. How do I make it search for all words?
SELECT *
FROM Event
WHERE about LIKE 'strokestown%'
OR about LIKE 'arts%'
OR about LIKE 'day%';
Thank you for your time!!
Jim
Place the wildcard character, '%', at the start as well as the end of the your search terms:
SELECT *
FROM Event
WHERE about LIKE '%strokestown%'
OR about LIKE '%arts%'
OR about LIKE '%day%';
SELECT * FROM Event
WHERE about LIKE '%strokestown%'
OR about LIKE '%arts%'
OR about LIKE '%day%'
Put a % before and after the keywords.
You can make this smaller like this: SELECT * FROM Event WHERE about REGEXP '(strokestown|arts|day)'
SELECT * FROM Event WHERE about LIKE '%strokestown%' OR about LIKE '%arts%' OR about LIKE '%day%';