If I have a table files and it has a column title, and some of the titles are in this format:
google: and facebook
stack: overflow
Now I'm trying to add search functionality in my app, which executes a LIKE '%word%' query. But if people search google and facebook it doesn't find anything, unless they specifically search for google: and facebook.
I know what LIKE does and why it doesn't give the results I'm looking for, I'm just asking if there's a way to search in mysql table and ignoring special chars like : ' - . , " etc.
Thanks.
Use REPLACE function prior to comparison.
... REPLACE( fieldname, ':', '') LIKE %word%
Alternative is to replace all spaces in the search string with '%' or you explode the search string (space as separater) and connect the different parts with an OR statement.
You can work with regular expressions
MYSQL Manual
Related
I have a book titled "Web Developer", "Web Designer" and I want a query that will return both book when I searched anything that's has "web" word in it for example "Web Consultant". But the problem is when I query like this it's not returning any data
SELECT * FROM `books` WHERE `title` LIKE '%Web Consultant%';
but I know changing the like value to '%web%' will return all data with "web" world in it. But my problem is what if the user add some words after the "web" word.
So the question is what is the exact approach or query for doing it, thanks!
Example: the scenario here is what if a user is searching for something that he can't remember the full title but only a word in it. So he will try to search like "web blahblablah" so the goal here is I want to return all books that has a "web" word on their title's
what if the user add[s] some words after the "web" word
If you want a list of books whose title contains "web" followed by some unknown word, then you can execute something this:
SELECT * FROM `books` WHERE `title` REGEXP 'web[[:blank]]+[[:alpha:]]+'
Of course, it depends on how you define word as to what follows the + in the above RE. But I think the point of your question is that you want to exclude books whose title ends with "web" because you know that there must be some word after it. You can look here for more details on how you can define word.
You should keep in mind that you asked to find "web" followed by some unknown word. If you had said you wanted to find the word web followed by some unknown word, then the RE would be more complex. You would have to allow the "w" to be either at the beginning of the title or be preceded by a whitespace character.
Regular expressions to the rescue, though the details vary across implementations. I've been using Oracle lately, but reviewing MySql, if you don't need multi-byte characters, I come up with:
SELECT * FROM `books` WHERE title REGEXP 'Web Developer|Web Designer'
https://dev.mysql.com/doc/refman/5.5/en/regexp.html
I suggest to handle the input before query:
Explode the input to an array by blank. PHP for example:
$wordArray = explode(' ', 'Web Consultant');
Combine each word into OR conditions to a query:
SELECT * FROM books WHERE title LIKE '%Web%' OR title LIKE '%Consultant%';
This is a simple pattern what search engine does.
In your case, you could also define the first word to 'Web%', which makes more accurate for finding a title pattern.
I want to write an SQL query that will fetch all the students who live in a specific Post Code. Following is my query.
SELECT * FROM `students` AS ss WHERE ss.`postcode` LIKE 'SE4 1NA';
Now the issue is that in database some records are saved without the white space is postcode, like SE41NA and some may also be in lowercase, like se41na or se4 1na.
The query gives me different results based on how the record is saved. Is there any way in which I can handle this?
Using regexp is one way to do it. This performs a case insensitive match by default.
SELECT * FROM students AS ss
WHERE ss.postcode REGEXP '^SE4[[:space:]]?1NA$';
[[:space:]]? matches an optional space character.
REGEXP documentation MySQL
Whether case matters depends on the collation of the string/column/database/server. But, you can get around it by doing:
WHERE UPPER(ss.postcode) LIKE 'SE4%1NA'
The % will match any number of characters, including none. It is a bit too general for what you might really need -- but it should work fine in practice.
The more important issue is that your database does not validate the data being put into it. You should fix the application so the postal codes are correct and follow a standard format.
Use a combination of UPPER and REPLACE.
SELECT *
FROM students s
WHERE UPPER(REPLACE(s.postcode, ' ', '')) LIKE '%SE41NA%'
SELECT * FROM students AS ss
WHERE UPPER(REPLACE(ss.postcode, ' ', '')) = 'SE41NA' ;
SELECT *
FROM students AS ss
WHERE UPPER(ss.postcode) LIKE SELECT REPLACE(UPPER('SE4 1NA'), ' ', '%'); ;
I propose using the spaces replaced with the'%' placeholder. Also transform the case to upper for both sides of the LIKE operator
I have the following query:
SELECT item from table
Which gives me:
<title>Titanic</title>
How would I extract the name "Titanic" from this? Something like:
SELECT re.find('\>(.+)\>, item) FROM table
What would be the correct syntax for this?
By default, MySQL does not provide functionality for extracting text using regular expressions. You can use REGEXP to find rows that match something like >.+<, but there is no straightforward way of extracting the captured group without some additional effort, such as:
using a library like lib_mysqludf_preg
writing your own MySQL function to extract matched text
performing regular string manipulation
using the regex functionality of whatever environment you're using MySQL from (e.g. PHP's preg_match)
reconsidering your need for regular expressions entirely. If you know that all your rows contain a <title> tag, for instance, it may be a better idea to simply use "normal" string functions such as SUBSTRING
As pointed out in the informative answer by George Bahij MySQL lacks this functionality so the options would be to either extend the functionality using udfs etc, or use the available string functions, in which case you could do:
SELECT
SUBSTR(
SUBSTRING_INDEX(
SUBSTRING_INDEX(item,'<title>',2)
,'</title>',1)
FROM 8
)
from table
Or if the string you need to extract from always is on the format <title>item</title> then you could simple use replace: replace(replace(item, '<title>', ''), '</title>','')
This regex: <\w+>.+</\w+> will match content in tags.
Your query should be something like:
SELECT * FROM `table` WHERE `field` REGEXP '<\w+>.+</\w+>';
Then if you're using PHP or something similar you could use a function like strip_tags to extract the content between the tags.
XML shouldn't be parsed with regexes, and at any rate MySQL only supports matching, not replacement.
But MySQL supports XPath 1.0. You should be able to simply do this:
SELECT ExtractValue(item,'/title') AS item_title FROM table;
https://dev.mysql.com/doc/refman/5.6/en/xml-functions.html
I am currently improving the search functionality of my cms so that users can search for entries by copying and pasting text from a web page and finding it in the database.
The query is simple. It takes the search term and does a LIKE '% text here %' query.
The problem is, I'm not getting many results and have figured out why.
In the CMS itself, a lot of the text that has been entered from MS Word seems to be double spaced. Such as
"Hello my name is James"
However on the front end website it renders properly, with single spaces, like:
"Hello my name is James"
This means my query is never picking up the database entry based on what is shown on the web page.
Any suggestions? Do I tackle the double spaces in the CMS (seems risky to me with so much HTML in there!), or can I adjust my query to cope with it?
if it is only double spaces that are creating the issue, then just
replace(columnToSearch,' ',' ')
when searching, or as #ManMohan suggests, before inserting the data into your table in the first place
Take a look at MySQL REPLACE
REPLACE(str,from_str,to_str)
Returns the string str with all occurrences of the string from_str replaced by the string to_str.
REPLACE() performs a case-sensitive match when searching for from_str.
I have an InnoDB database table of 12 character codes which often need to be entered by a user.
Occasionally, a user will enter the code incorrectly (for example typing a lower case L instead of a 1 etc).
I'm trying to write a query that will find similar codes to the one they have entered but using LIKE '%code%' gives me way too many results, many of which contain only one matching character.
Is there a way to perform a more detailed check?
Edit - Case sensitive not required.
Any advice appreciated.
Thanks.
Have a look at soundex. Commonly misspelled strings have the same soundex code, so you can query for:
where soundex(Code) like soundex(UserInput)
use without wildcard % for that
SELECT `code` FROM table where code LIKE 'user_input'
thi wil also check the space
SELECT 'a' = 'a ', return 1 whereas SELCET 'a' LIKE 'a ' return 0
reference