Thanks for taking time to read my question.
I have created a MySQL table, a HTML form and a program in PHP which connects the form to MySQL table and retrieves sequences for column Annotations which is text data type.
This column has characters and also has one or more of hyphen, comma, parentheses, period or spaces.
Please look at the following code that I used for select query:
$values=mysql_query("SELECT Sequence
FROM oats
WHERE Foldchange = '$Foldchange' AND
RustvsMockPvalue = '$RustvsMockpvalue' AND
Annotations REGEXP '%$Annotation%[-]+'");
Here $Annotation is the form variable which holds the value entered by the user in the form. Annotations is the column name in the MySQL table.
Annotations column has characters A-Z or a-z and one or more of hyphen, comma, space or parentheses like the following.
Sequence is another text column in the MySQL table but does not have ,./().
Example data from Annotations column:
ADP, ATP carrier protein, mitochondrial precursor (ADP/ATP translocase) (Adenine nucleotide translocator) (ANT).
I am not able to retrieve Sequence column data when I search for any Annotations column data with comma, parentheses, period and slash. It works fine for those records which does not have these ,.()/.
I tried to use LIKE instead of REGEX but it didn't work either.
A record from mysql table:(columns that you see below: contigid,source,genelength,rustmeans, mockmeans,foldchange,pvalue,rustmockteststatistic,Annotations and Sequence)
as_rcr_contig_10002 ORME1 2101 506.33 191 -2.18 2.21E-10 -6.35 Tesmin/TSO1-like, CXC domain containing protein. AACAATTCCCCTCAACCAACCTTTTATTTCATCCCATTTTTATCATCTGTCCGGTTACAGATTTTGCTTCCAGTTAGGTGCCACTTCTTCAAACGCTCAACCCTTACCCACTACCACCCCACCAAAACCAACCCCCCAAGATGCAGTTCATCACTCTCGCCGTTGCTTTTGCTTTCTTTGCTGGTGCCANNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNCTTTTGCTTTCTTTGCTGGTGCCACCTCGTCGCCGGTTTCCATGGACCCCAAAGCCGAGAAGTCCGGCTCCTCGGGATCCGGTGGCGCCCCTCTGGGCACTGCTAGCCCCTATCCCCAAAGNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNGGTGGCCCTCAGTCGCCAGGCTCTGGCCAACCCGGTAGGATGCCATGGGGTAGCGACCAATCTGCCTACGGTGGTGGTTTCCCTTATGGATCATTCCCCTCGGTTTCGGGGCAATCCCAATCGACGGCCTATGCTCAAGCTCAATCATCCAGTTTCCCCTCAAACGGTGTCCCGACACACTCCTCGGCCTCCGCCCAAGCGCAATCATCCGGTCCTGGACAAGCTCAGGCAGCCGCTTCTGCCCAGGTTCCCGGCGGCCCCCACGGTCAAGGTTCTAACGGATTTGGCGCACAAGGCCAGTTTGGACAGAACGGGCAGAACGGCCTCTATGGTCAAGACGGCAATGGCTTTAGTGCCCAAGGCCAATTTGGACAGAGTGGACAGAATGGCTTCTATGGTCA
Could someone please help me in the correct syntax of the SELECT syntax? Thank you.
You need to familiarise yourself with regex - it's its ownittke language.
Use REGEXP with the right regex:
WHERE ...
AND Annotations REGEXP '[-A-Za-z(). ]+'
AND Annotations NOT_REGEXP '[A-Za-z]+'
If mysql supported regex look aheads, this could be done in one test.
,
First of all, you are not using REGEXP properly.
You should check the differences between LIKE and REGEXP.
REGEXP use Regular expresions, which have very particular syntax.
LIKE use simple text remplacement with key characters like % or _
Here you are using REGEXP with %, that's why it's not working. % is a key character for LIKE only.
But in REGEXP, . and - are special characters that you need to escape to.
If you want to check several characters, REGEXP is the way to go :
Annotations REGEXP '.*$Annotation.*[\-(),\.]+.*'
This match :
.* : 0 to n characters
$Annotation : Your keyword
.* : 0 to n characters
[\-(),\.]+ : At least 1 character from the list : - ( ) , .
.* : 0 to n characters
Tell us if that match your data.
Since we can't craft a Regular Expression that would work in your case without getting into some crazy matching schemes (orders and so forth), In order to find what you're looking for, you'll need to custom construct the SQL statement and luckily you're using PHP.
Here I'm starting with a simple space delimited entry. Remember that you can't wrap something with parenthesis because the parenthesis might not match up in your result set.
$search_input = 'ADP ANT';
//example of array from a search page full of check boxes or fields
$annSearches = explode(' ',$search_input);
/*annSearches is now and array with ADP,ANT*/
$sql = "SELECT Sequence FROM oats WHERE Foldchange = '$Foldchange' AND RustvsMockPvalue = '$RustvsMockpvalue'";
foreach ($annSearches as $Annotation){
$sql .= " AND Annotations LIKE '%$Annotation%'";
}
The output SQL statement would look like this (wrapped for clarity):
SELECT Sequence FROM oats WHERE
Foldchange = '$Foldchange'
AND RustvsMockPvalue = '$RustvsMockpvalue'
AND Annotations LIKE '%ADP%'
AND Annotations LIKE '%ANT%';
If you do a really long query, this will get slower and slower as MySQL has to run through every record in the database over and over for the results.
FULLTEXT SEARCH OPTION
Another way that you could potentially do this is to enable FULLTEXT search functionality on the Annotations field in the table in the database.
ALTER TABLE oats ADD FULLTEXT(Annotations);
This would allow you to do a search something like this:
Sequence FROM oats WHERE
Foldchange = '$Foldchange'
AND RustvsMockPvalue = '$RustvsMockpvalue'
MATCH(Annotations) AGAINST ('ADP ANT')
Related
I have a databse with keywords coloumn
Need to search the database on the basis of query done by user.
Every keyword has word "outlet" at the end but user will only search "gul ahmad" not "gul ahmad outlet". For this i used following query and things worked fine to get results and found complete result "Gul Ahmad Outlet"
$sql = "SELECT keywords FROM table WHERE keywords REGEXP '([[:blank:][:punct:]]|^)$keyword([[:blank:][:punct:]]|$)'";
Now i have 2 issues
1. If the word "outlet is in between the query words then it does not find the word. e.g if user search "kohistan lahore", database has an outlet named "kohistan outlet lahore" but it does not find the keyword in database and returns empty. How to tell database to include "outlet" in between, at the start or athe end to find and match the result.
if some user search "nabeel's outlet" database has it but due to " ' " this query returns empty without any result.
What you can do is that you can match your column values with just the first word
of your search expression(i.e nabeel's outlet). I believe this way you will be able to cover all your scenarios.
select
*
from `outlets`
where REPLACE(`name`,'\'','') regexp SUBSTRING_INDEX('nabeels outlet', ' ', 1)
Look at this fiddle and test yourself : http://sqlfiddle.com/#!9/b3000/21
Hope it helps.
Much simpler: [[:<:]]$keyword[[:>:]] -- This checks for "word boundary" instead of space or punctuation or start/end of string. And $keyword = "nabeel's" should not be a problem.
Don't you want to always tack on "outlet"?
REGEXP "[[:<:]]$keyword[[:>:]] outlet"
And, yes, you must escape certain things, such as the quotes that will be used to quote the regex string. PHP's addslashes() is one way.
I have this issue with mysql when querying a DB inside PHP.
The PHP code is:
$Query = "SELECT COUNT(*) FROM theTable WHERE fieldValue REGEXP 'Dom-R[eéèêë]my'";
$DBR = mysql_query($Query,$Connection);
I am expecting this query to get things like, I mean find the number of those:
Dom-Remy
Dom-Rémy
Dom-Rèmy
...etc...
But I get nothing, I mean zero. What is wrong in the code? I have tried several variations, all equally not working.
This is subject of Unicode characters.
What happens is that e,é,è,ê,ë.. in your example is not a single letter but 2 because the tilde counts as a character as well. This brings lots of complexities and rules that needs to be followed in order to meet Unicode rules.
You could do something like: ([\x{0049}-\x{0130}]) to search letters with tildes but this expression may vary depending if you are going to use this expression on .net, java, javascript or php.
You could also check what code each character represents here:
http://www.fileformat.info/info/unicode/char/search.htm?q=%C4%B0&preview=entity
As per official website specification, MySQL regex is matched in byte-wise fashion
The REGEXP and RLIKE operators compare characters by their
byte values and accented characters may not compare as equal even if a
given collation treats them as equal.
If you can match any character in place of [eéèêë], this should be sufficient:
$Query = "SELECT COUNT(*) FROM theTable WHERE field REGEXP '^Dom-R.+?my$'";
If
the column's CHARACTER SET is utf8 or utf8mb4, and
your connection between client and mysql server is also either of those character set, and
you are not using COLLATION utf8_bin, then
'Dom-Remy' = 'Dom-Rémy' = ...
WHERE ... = ... and WHERE ... LIKE ... will abide by the above. REGEXP (RLIKE) cannot be used, for the reasons already discussed.
This shows what is equal (for = and LIKE.)
If you are simply searching a string for Dom-Remy, use
fieldValue LIKE '%Dom-Remy%`
and instead of regexp/rlike
If you have something more complex that needs REGEXP, then start a new question with the details.
I have a table where a column allows special characters like / , \ , # , $ .
Now when I am trying to search such records from table, I am unable to get.
I have tried one query but it is returning 0 rows but actually there is 1 record existing. How to write a query in this case ?
My query (which is giving the wrong result) was something like this
select * from mytable where column7 like '%jk\xyz#%'
You may need to escape some special characters - particularly backslash. Note that with the LIKE operator - there is a two part process to issuing the command & both strip escaping - so to search for a single backslash you need to use 4 backslashes.
select * from mytable where column7 like '%jk\\\\xyz#%'
See this : MySQL LIKE operator with wildcard and backslash
and http://dev.mysql.com/doc/refman/5.6/en/string-comparison-functions.html#operator_like
Not sure if it is actually possible, but consider the following text:
INSERT INTO cms_download_history
SET
user_id = '{$userId}',
download_id = '{$fileId}',
remote_addr = '{$remote_addr}',
doa = GetDate()";
I want to change that to be:
INSERT INTO cms_download_history
(user_id,download_id,remote_addr,doa)
VALUES('{$userId}','{$fileId}','{$remote_addr}',GetDate());
Doing a regex to find and replace this one is easy as I know how many columns I have but what if I am trying to do this for multiple similar queries without knowing the number of columns, i.e.:
INSERT INTO mystery_table
SET
col1 = val1
col2 = val2
.... unknown number of columns and values.
Is there a dynamic regex that I can write that would detect that example?
Actually, if all queries look like this, with only a variable amount of columns, you can get the field names using a somewhat simple regex:
(\w+)\W*=\W*['"].+?(?!\\)['"],
Here is an example. Here is what it does:
It captures one or more word characters, if followed by:
Zero or more whitespace characters
An equal sign
Zero or more whitespace characters (again)
A ' or " (start of a String)
One or more characters
An unescaped ' or "
A comma
Note that this does assume that all values are strings. If you also need support for numbers, please let me know.
Alright, I've had a some nightmares with it already so i bow my stupid head to the almighty hive-mind of the Stackoverflow.
UPD: the parameters of my task have been changed. I need to find the codes that could include special chars like parentheses (), and/or #, #, &, $.
So the codes could look like:
"A", "Bb", "2C8", "A7(BO)19", B29H$, 29H(6JI)0# etc
The problem is that all these codes are optional. I've tried like Barmar (see reply 1) suggested, but slightly modifing the MySQL query:
SELECT *
FROM table
WHERE column REGEXP '^[a-z0-9\#\#\$\&()]*$'
AND column LIKE CONCAT('%', '$v', '%')
It cannot return me "S(LJ)9" or "09S(LJ)3$" if i seek for "SLJ" or "S(LJ)"
Well, aside some real important nucleotide sequence in my DNA that would allow me to use brains more efficiently (or have them), what am i missing in this regex or the query itself?
Thanks anyone in advance.
SELECT *
FROM table
WHERE column REGEXP '^[a-z0-9()]*$' /* code contains alphanumerics and parentheses */
AND column NOT REGEXP '[0-9]{3}' /* code does not contain >2 digits in a row */
AND column LIKE CONCAT('%', ?, '%') /* code contains the user input */
where ? is bound with the user input (you should be using PDO or mysqli prepared statements).