When I searched for citroen in search page like that I get result but if I search blackn roll I dont get result because it's written like black'n roll in the table. Some user may also wanna search blackn roll but doesnt get result. How can I fix it? And also rows like v-hr and speacial characters like "&/(). I want the mysql to ignore them.
$sql = "SELECT * FROM arac
INNER JOIN suv_marka ON arac.marka = suv_marka.id
WHERE match(suv_marka.marka) against('citroen')";
If you know the characters (to be replaced) already then, you can use MySQL's REPLACE function to replace them with % and perform LIKE comparison, e.g.:
create table test(value varchar(100));
insert into test values ('black''n roll');
SELECT value
FROM test
WHERE 'blackn roll' LIKE CONCAT('%', REPLACE(value, '''', '%'), '%');
You can replace 'blackn roll' with your input string and use nested REPLACE functions if you need to replace more than one character.
The back-slash is MySQL's escape character. You can try the following... Full text indexing gets a little weird because of word terminators.
$sql = "SELECT * FROM arac
INNER JOIN suv_marka ON arac.marka = suv_marka.id
WHERE match(suv_marka.marka) against ('black\'n roll' IN BOOLEAN MODE);"
If that doesn't work, then try looking for the words individually. Word length is also a factor, small words (less than 4 characters by default) are not indexed.
$sql = "SELECT * FROM arac
INNER JOIN suv_marka ON arac.marka = suv_marka.id
WHERE match(suv_marka.marka) against ('black' + 'roll' IN BOOLEAN MODE);"
Related
I can't get the REPLACE function to work on my SQL command shown here. I want it to take my search term (55512) and search for this in the field using the modified field from the regexp to remove all non-alphanumeric fields.
SELECT
`Customers`.`id` AS `Customers__id`,
`Contact`.`id` AS `Contact__id`,
`Contact`.`customer_id` AS `Contact__customer_id`,
`Contact`.`full_name` AS `Contact__full_name`,
`Contact`.`phones` AS `Contact__phones`
FROM
`customers` `Customers`
INNER JOIN `contacts` `Contact` ON `Contact`.`id` = (`Customers`.`contact_id`)
WHERE
REPLACE(
`Contact`.`phones`, "[^a-zA-Z0-9]",
""
) LIKE '%55512%'
LIMIT
20 OFFSET 0
So what I want to do is be able to search "55512" and have it match if the Contact.phones field contains "999-555-1212" or "(999) 555-1212". The Contact.phones field is stored as a JSON array in case this is relevant, but I would expect the above SQL command to remove all brackets, curly braces, etc and just search for the string.
When I do the above search in MySQL, it returns zero results, but there is a result that contains: [{"value":"999-555-1212","label":"Primary"}]
The problem is that the REPLACE function does not work with regex, but attempts to match strings.
You can solve this problem in two ways:
adopting REGEXP_REPLACE function, that effectively uses regex:
WHERE
REGEXP_REPLACE(
`Contact`.`phones`, "[^a-zA-Z0-9]",
""
) LIKE '%55512%'
keeping the REPLACE function, but replacing dashes only with the empty string.
WHERE
REPLACE(
`Contact`.`phones`, "-",
""
) LIKE '%55512%'
Check the demo here.
WHERE phones RLIKE '555-?12'
Would also work since the only extraneous characters would be a dash in one spot.
Both this and the answer from lemon will require a full table scan. That is, either will be slow if the table is big.
Using SQL to match characters in query string
MySQL SELECT query string matching
These do not match my requirement.
Hi, I am using PHP with mysql. I have a 'job search' functionality in my website, where we can search jobs by city.
For example if we post a job in Hyderabad city, and if a user search for the job by using the same word 'Hyderabad' he gets the results. But if he searches by typing Hydrabad('e' is missing) he cannot get the results and also same problem with 'Delhi', 'New Delhi', 'NewDelhi'.
If I use wild card operators like,
"SELECT * FROM jobs_tbl WHERE job_city LIKE '%".$search_city."%' "
"SELECT * FROM jobs_tbl WHERE job_city LIKE '%%".$search_city."%%' "
they wont work for me because I shoud get results though some letters and spaces are missed in the string.
So, can you please tell me if there is any possibility to fetch a row if atleast 4 or 5 characters are matched with the search string? I searched for such operator but could not find any. Or is there any alternative way?
You can put % between each character.
$like_city = preg_replace('//', '%', $search_city); // Hyderbad => %H%y%d%e%r%b%a%d%
$sql = "SELECT * FROM jobs_tbl WHERE job_city LIKE '$like_city' "
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.
EDIT : 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
SELECT * FROM table WHERE search (LIKE %searchterm1%) OR (LIKE %searchterm2%) OR (LIKE %searchterm3%) ....
The the words are unordered use a standard logical conjunction (aka AND)
LIKE '%word1%' AND LIKE '%word2%'
If the words are ordered use an implicit conjunction in the search term itself
LIKE '%word1%word2%'
Modify the like wildcards (and quotes) as needed; also consider if a full-text search might be more appropriate.
The correct syntax is;
SELECT * FROM table WHERE (column1 LIKE '%text%' AND column1 LIKE '%field%')
To allow the user to input multiple words, firstly take a look at the problems of SQL injection, but assuming you're using PHP you can explode an input string and implode the resulting array, like this;
$values = explode(" ", $input); // delimiter is a space
$query = "SELECT * FROM table WHERE (column1 LIKE '%" . implode("%' AND column1 LIKE '%",$values) . "%')";
Hope this helps!
I got a problem regarding SQL query. If single word SQL query can detect the string, but if I add multiple words inside the string, it won't show any results.
SELECT * FROM rules WHERE keyword LIKE CONCAT('%','$message','%')
Input 1 (show results)
$message = "ASK"
Input 2 (show no results)
$message = "I WANT TO ASK"
I'm assuming you want those rows where the value of the keyword column is part of the message. That's the typical use case for key words.
If that's right then you've get this result with the help of the function INSTR:
SELECT * FROM rules WHERE INSTR('$message', keyword) > 0;
While
SELECT * FROM rules WHERE '$message' LIKE CONCAT('%', keyword, '%');
would work most times too, it's got the restriction that the keyword musn't contain the wildcard characters for the LIKE operator: % and _. INSTR doesn't have this restriction.
I'm trying to puzzle out how to add a LIKE to this prepared statement.
SELECT convention_name FROM events_master
WHERE (convention_name = ?);
The goal is to see if the convention name is similar to something that already exists in convention_name. I do understand that it checks for an exact match already.
Thanks in advance.
SELECT convention_name FROM events_master
WHERE (convention_name LIKE 'partofname' + '%');
The % character is a wild char so if you put it in the back it will search for anything that begins with 'partofname' + blah appended to it.
if it's Like = '%partofname%' then partofname could have characters before or after it.
If SQL LIKE clause is used along with % characters, then it will work
like a meta character (*) in UNIX while listing out all the files or
directories at command prompt.
Without a % character, LIKE clause is very similar to equals sign
along with WHERE clause.
$sql = $dbh->prepare(SELECT convention_name FROM events_master
WHERE convention_name LIKE ?");
$sql->execute(array('%'.$yourSearchString.'%'));
For example:
String query = "SELECT convention_name FROM events_master WHERE convention_name like ?";
PreparedStatement stm = conn.prepareStatement(query);
stm.setString(1, "%car%");
Your argument may contain wildchar %, then it will find for example card, scart ..
If this substring match is not enough, then you can use soundex algorithm to find similar strings. See how to compute similarity between two strings in MYSQL for more options.