SQL Query like that: "some string" LIKE "%"+column+"%" - mysql

I've got a table full of substrings, and I need to select all substrings, which are in the search string.
Basically it would be the mirrored form of a where-condition:
WHERE "SearchString" LIKE "%"+ currentColumnValue +"%"
I know, that this is not possible, but for performance reasons I don't want to iterate every single database entry.
Maybe you have got an idea how to solve this kind of problem?

You can do
where 'SearchString' like concat('%', columnValue, '%');
This will be very slow as it would do a table scan and a like-compare on each line, but the result is correct.
http://sqlfiddle.com/#!2/e2b066/1

You can try this
$value = //your value
' select * from tb_name where col_name like %".$value."% '

Related

MySQL LIKE for two words

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!

How do I query for fields containing a given text in MySQL?

SELECT stuff REGEXP 'itunes' as is_itunes;
In this MySQL query, if "stuff" has the word "itunes" in it, it will mark it as itunes.
However, I want to say "begin with". How can I check for "begin with" instead of anywhere in the text?
SELECT ... WHERE stuff LIKE 'itunes%';
Here % serves as a wildcard character, so this would match rows with the stuff field equal to any of itunes, itunesfoo, itunes1, ...
More info: SQL LIKE Operator at W3Schools.
Add the caret symbol, it represents the beginning of the string:
SELECT stuff REGEXP '^itunes' as is_itunes;
However, LIKE 'itunes%' as suggested by Marc should be a lot faster, especially if your indexes are set up correctly.
SELECT ... WHERE LEFT(stuff, 6) = 'itunes';
For an indexed field it is much better to do:
select ... where stuff >='itunes' AND stuff < 'itunet'
This will not create a full table scan, and use the index.
SELECT ... WHERE stuff REGEXP '[[:<:]]itunes';

Easy way to update certain field in database that is upper case?

I am looking to update the field 'prefix' in my course table to just have the FIRST letter capitalized and not the whole prefix.
Is there any easy way to do this is SQL?? Sample Output could look like 'Aadm' in the database for 'prefix'.
My table looks like:
Sample rows look like:
I have SQL that looks like:
WHERE CONCAT(prefix,code) LIKE '%". $keywords . "%'");
Is it possible to user LOWER on prefix here?
select prefix,
concat(upper(substring(prefix,1,1)),substring(lower(prefix) from 2)) as initcap
from course
try it in select form before update your field
if you're looking for prefixes where all chars are upper case use a regexp
where binary(prefix) regexp '^[A-Z]+$'
EDIT. Update query
update course
set prefix =
concat(upper(substring(prefix,1,1)),substring(lower(prefix) from 2))

Issue using LIKE in a SQL query with special characters in it

SELECT id, absolute_tag, tag FROM c7_storage_tags
WHERE users_id = 1
AND LOWER(absolute_tag) LIKE LOWER('##Pictures#zee\'s fav%')
This is not giving me the correct result, even thou i do have a row in my database with the correct string.
The better solution for me would be to figure out a way so that "##Pictures#zee\'s fav" string of mine, could be treated as just a string. And I can perform like on it.
How can I fix it.
Thanks in advance.
Zeeshan
IF you string has a backslash in it, you should escape it twice :
// value absolute_tag == "##Pictures#zee\'s fav"
select * from test where `absolute_tag` like '##Pictures#zee\\\\\'%'
From http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
Your query should look like
SELECT id, absolute_tag, tag FROM c7_storage_tags
WHERE users_id = 1
AND LOWER(absolute_tag) LIKE LOWER('##Pictures#zee\\\\\'s fav%')
Your original query was looking for a record with absoulte_tags begins with "##Pictures#zee's fav"

mysql string contained within a search string

What I need seems simple, but I haven't been able to pull it off so far. Maybe it's just these late hours, or maybe it's not that simple after all, I don't know any more :)
So, here's the thing. I want to be able to check whether the search string from my site contains any of the fields from a particular column in my database. So, it would be the opposite of the usual one:
mysql_query("
SELECT *
FROM `table`
WHERE `column` LIKE '%{$search}%'
");
which looks for the fields with values where the search string is contained.
What would be the easiest way, using some regular expressions or...?
Thanks a bunch!
Just do it the other way around
SELECT *
FROM `table`
WHERE '{$search}' LIKE concat('%', `column`, '%')
bear with me for the proper syntax for variable escaping for SQL-injection.
your query is also good as you want.
but still you can try this.
You can search your string by using MATCH() AGAINST() statement in mysql.
mysql_query("SELECT * FROM table_name WHERE MATCH(field11,field12) AGAINST ('searchstring')");
Here your field must be having Fulltext datatype.
This may work good.
Thanks.