I'm in trouble with a strange behavior of mysql.
If I try to search with fulltext query sam* word, I have no results back.
Ex. table companies
id || name
1 || Company name
2 || Same Company name
If I make
`SELECT name FROM companies WHERE MATCH name AGAINST ('+Company*' IN BOOLEAN MODE)`
It works, instead if I use
`SELECT name FROM companies WHERE MATCH name AGAINST ('+Sam*' IN BOOLEAN MODE)`
or
`SELECT name FROM companies WHERE MATCH name AGAINST ('+Same' IN BOOLEAN MODE)`
I have no results!
Do you have any idea about this issue?
PS. if I use +Sa* instead of +Sam*, it works
You must to change the configuration value ft_min_word_len (fulltext min word length) the default value is set to 4.
Add (or modify) this line to mysql config /etc/my.cnf (or my.ini in windows)
ft_min_word_len = 3
After that you may call the index re-creation using the command:
REPAIR TABLE companies QUICK;
BTW: Fulltext search doesn't accept LIKE wildcards: * or %
Related
I have a MySQL table and on of the column that contains username in different patterns. I am wondering is there a way to pull the usernames that (first 3 positions are in letter and last 4 positions are in number)? So I want to pull ABE0005, LKD0125, GHD0011, etc... See below for username examples.
username column:
ABE0005
LKD0125
905593580
GHD0011
903649229
BAN_SS_USER
This is how I pull data for username that starts with 9.
$query = mysqli_query($db, "SELECT * FROM `table_name` WHERE term = '2022' AND user LIKE '9%'");
$b_202310_3 = mysqli_num_rows($query);
Use regexp in a where clause to achieve this.
where username regexp '^[A-Z]{3}[0-9]{4}$'
^ is the start of the string
[A-Z] is any uppercase alpha character
{3} is 3 characters of the preceding group/class
[0-9] is any single integer
{4} is 4 of previous group/class
$ is the end of the string
Demo: https://regex101.com/r/LFFc4f/1 (Note this tool is using PCRE, MySQL uses POSIX (e.g. something like \d won't work in MySQL but would in this tool))
Per update query should be:
SELECT *
FROM `table_name`
WHERE term = '2022' AND user REGEXP '^[A-Z]{3}[0-9]{4}$'
I Have a string foo = "a,b". Now I want to search in the mysql database to get user_id while comparing the string to the likes field. The likes field has data in the format interest => a c v d b.
The different characters are seperated by a space. I tried Using like but the result was not upto the mark. How can I go about it?
This is my code
select user_id from users where interest like %foo%;
MySql does not support multiple keyword search in set like field, you should add OR condition of each search keyword with REGEXP
if your format interest like=> a,c,v,d,b then you can use FIND_IN_SET() function otherwise REGEXP provide to exact search.
SELECT user_id FROM users
WHERE interest REGEXP '[[:<:]]a[[:>:]]' AND interest REGEXP '[[:<:]]b[[:>:]]'
this query search only a and b in field not aa, bbax
LIKE does not support exact search.
I have a mysql query as follows.
$query="SELECT name,activity FROM appid
where result > 5 AND name LIKE :term ORDER BY name ASC LIMIT 0,40";
$result = $pdo->prepare($query);
$result->bindvalue(':term','%'.$_GET["q"].'%',PDO::PARAM_STR);
$result->execute();
What i want to do is this.
I have and entry like this that i want to find
'News & Weather'
However when i type
'news weather'
it of course will not find it. How can i be able to type that and retrieve that entry?
Regular expressions can do the trick:
select *
from appid
where name rlike 'news|weather' -- Matches 'news' or 'weather'
Another example:
select *
from appid
where name rlike 'news.*weather' -- Matches 'news' and 'wether'
-- with any characters in-between or none at all
-- (ordered)
Just one more:
select *
from appid
where name rlike '^news.{1,}weather$' -- Matches any text that starts with 'news'
-- and has at least one character before
-- ending with 'weather'
Regular espressions can be used to create very complicated filters on text fields. Read the link above for more information.
If you can add a full-text index to your table, Full-text search might be the better way to go with this. Specifically, a boolean Full-Text search:
select *
from appid
where match(name) against (+news +weather)
I believe the only way possible are through code:
Option A: Replace the spaces in your query parameter with '%' in code, but that of course will make the multiple words ordered
Option B: Split your parameter on spaces and dynamically construct your query with as many LIKEs as needed, adding additional ":termN" parameters for each one.
Table
id name
--- ------
1 chinu
2 sanjib
3 chinmay
My MYSQL Query
SELECT * FROM users WHERE MATCH (name) AGAINST ('chi' IN BOOLEAN MODE)
In above query i am getting 0 record.
My output will be coming
1 chinu
3 chinmay
How to get my actual record using MATCH...AGAINST query?
EDIT - If i am searching chinu instead of chi i am getting 1 record.
You need to add an asterisk to the 'chi' to indicate that the query should match against all that contain the string and not just the string itself. Just using the string 'chi' will only match exactly 'chi' for example.
change your query to read
SELECT * FROM users WHERE MATCH (name) AGAINST ('chi*' IN BOOLEAN MODE)
and you should get the results you expect.
I think you forgot the + sign:
SELECT * FROM users WHERE MATCH (name) AGAINST ('+chi' IN BOOLEAN MODE)
Or if it is an exact phrase, use double quotes to surround the string:
SELECT * FROM users WHERE MATCH (name) AGAINST ('"chi"' IN BOOLEAN MODE)
I am the first to admit that this is not easy to find. MySQL full text search uses a system variable called ft_min_word_length. The default value is 4, as shown here.
Because you are searching for a 3-character word, it is not being indexed. Hence it is not found.
More information is available in the documentation on fine tuning the search. But the basic idea is that you need to change the value of the parameter and rebuild the index.
For your particular query, though, you just need to include wildcards, as explained in other answers.
I have a fulltext index on a number of columns and i'm trying to do a MATCH AGAINST IN BOOLEAN MODE on those columns, trying to find an email address. Here are the results:
if i search for "test#email.com" (with quotes) - the query returns correct results
if i search for "a#b.com" (with quotes) - the query does not return anything
Can someone tell me why a short email a#b.com does not get returned and how would i solve this?
Here's the query i'm using:
SELECT MATCH(email, phone, title, description) AGAINST('"a#b.com"' IN BOOLEAN MODE) AS score
FROM thetable WHERE MATCH(email, phone, title, description)
AGAINST('"a#b.com"' IN BOOLEAN MODE) ORDER BY `status` DESC, score DESC
This is a combination of two problems:
# isn't considered being a 'word character', and neither is -, so searching for a#b.com actually comes down to searching for words a, b and com
a and b are shorter than ft_min_word_len
The solution would be to make # and . being considered word characters. There are several methods listed on http://dev.mysql.com/doc/refman/5.6/en/fulltext-fine-tuning.html
The most practical one would be adding a custom collation as described in
http://dev.mysql.com/doc/refman/5.6/en/full-text-adding-collation.html
Update:
a)You need to set ft_min_word_len = 1 in my.cnf.
b) Output of show variables
ft_min_word_len | 1
c) Fired below query:
mysql> SELECT name,email FROM jos_users WHERE MATCH (email) AGAINST ('a#b.com') limit 1;
+--------+---------+
| name | email |
+--------+---------+
| kap | a#b.com |
+--------+---------+
1 row in set (0.00 sec)
Hope this will help.
~K
I think you need to change ft_min_word_len
As specified in MySQLdoc fine tuning
The minimum and maximum lengths of words to be indexed are defined by
the ft_min_word_len and ft_max_word_len system variables. (See
Section 5.1.4, “Server System Variables”.) The default minimum value
is four characters; the default maximum is version dependent. If you
change either value, you must rebuild your FULLTEXT indexes. For
example, if you want three-character words to be searchable, you can
set the ft_min_word_len variable by putting the following lines in an
option file:
[mysqld]
ft_min_word_len=3
Then restart the server and rebuild your FULLTEXT indexes. Note particularly the remarks regarding myisamchk in the instructions
following this list.