This is sort of simplistic but I couldnt find anything that works for this particular situation.
Im trying to find a result via a Mysql query where the item in the db is one letter of a string. For example, I have the string 'MYSQL' and I need to retrieve everything in the DB with an identifier of M or and identifier of Q.
So the db looks like this
name identifier
item1 M
item2 Q
item3 B
and I want a search for 'MYSQL' to return item1 and item2.
A LIKE doesnt work so I tried a WHERE IN but that doesnt work unless I format the text like 'M','Y','S'...etc
which isnt out of the question but I have a feeling there is a more eloquent way to do it. Thanks in advance.
You may use this string function
SELECT * FROM table_name WHERE LOCATE(identifier, "mysql")>0
Use REGEXP.
If you create a regular expression with your single character identifier, you can match it to the word you want. For example:
select * from TABLENAME
where 'MySQL' REGEXP concat('.*', identifier, '.*');
That will get you the rows with 'M' and 'Q' but not the row with 'B'.
You might be doing your LIKE backwards.
In Oracle flavored syntax, you'd use: WHERE 'MYSQL' LIKE UPPER('%' || identifier_col || '%') where || is string concatenation and % is the wildcard character.
Why can't you use string concatenation and the LIKE operator?
SELECT name FROM table_identifiers
WHERE UCASE('MYSQL') LIKE UCASE(CONCAT('%', identifier, '%'))
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 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
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')
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).
I have this piece of code here:
$query = mysql_query("SELECT * FROM example WHERE text LIKE '%$value%'");
Would it make a difference if I would use:
$query = mysql_query("SELECT * FROM example WHERE text LIKE '$value'");
If yes, what would it be? What would be the difference?
Its not a difference in PHP, its a wildcard in SQL. You can read more about it here. Essentially, %
Matches any number of characters, even zero characters
Yes this has nothing to do with php, its a sql thing, % means like a wildcard there could be 0 or more characters instead of it.
%abc% matches abc, aabca, aabc, abcd
%abc matches dabc, abc but not abcd or tabcd
abc% matches abcd, abc but not dabc, tabcd
The difference is that '%$value%' will seach for matches containing $value (for exemple if $value = 'foo' it could return 'foobar' or 'barfoobar'), '$value' only matches the exact value of $value.
In PHP there is no difference (although you might want to take a look at using PDO for your database queries), the '%' symbols affect the query executed in MySQL.
% acts as a wildcard so the first result will return anything that contains the term 'value' in its text attribute, whereas the second will return only records that match exactly the term 'value'