MySQL magic column "name" and synthesized columns - mysql

So I got a SQL statement. The idea is that I want to do a case-insensitive LIKE.
I do it like this:
SELECT
FilenameId AS id,
LOWER(CONVERT(BINARY(Filename.Name) USING utf8)) AS name
FROM Filename
WHERE name LIKE '%something%'
COLLATE utf8_general_ci
This works fine, however my query also returns the case-transformed name. What I want to do
is synthesize the insensitive name and do a LIKE query on it, but also return the non case-transformed name.
SELECT
FilenameId AS id,
Filename.Name AS name,
LOWER(CONVERT(BINARY(Filename.Name) USING utf8)) AS iname
FROM Filename
WHERE iname LIKE '%something%'
COLLATE utf8_general_ci
...but then MySQL happily refuses:
Unknown column 'iname' in 'where clause'
What am I doing wrong? I am on MySQL 5.5 FWIW.

I don't know why you came up with this, but usually others have trouble getting LIKE case sensitive, not the other way round.
Write your query simply like this:
SELECT
FilenameId AS id,
Filename.Name AS name
FROM Filename
WHERE name LIKE '%something%'
And in general, you can't access aliases in WHERE clause. Either put your query into a subquery like Dhinakaran suggested or use HAVING (if you are lazy).
The difference? WHERE is rowbased, HAVING works on the result after applying WHERE clause (and GROUP BY).
From the manual:
The following two statements illustrate that string comparisons are not case sensitive unless one of the operands is a binary string:
mysql> SELECT 'abc' LIKE 'ABC';
-> 1
mysql> SELECT 'abc' LIKE BINARY 'ABC';
-> 0

Create a sub query then put where on subquery
Select * from (SELECT
FilenameId AS id,
Filename.Name AS name,
LOWER(CONVERT(BINARY(Filename.Name) USING utf8)) AS iname
FROM Filename ) temp
WHERE temp.iname LIKE '%something%'
COLLATE utf8_general_ci
Or
SELECT
FilenameId AS id,
Filename.Name AS name,
AS iname
FROM Filename
WHERE LOWER(CONVERT(BINARY(Filename.Name) USING utf8)) LIKE '%something%'
COLLATE utf8_general_ci

Related

How I get data in using LIKE without differentiating between uppercase And lowercase?

I'm trying to get names using Like '%BEJO%', but no record is found because my data in database is 'Bejo'.
How I do I get the name 'Bejo' with LIKE '%BEJO%'? (case insensitive)
Try this:
select t1.*
from table1 t1
where LOWER(column_name) LIKE LOWER('%BEJO%');
It seems the collation of that column is case-sensitive. If you want it to always be case-insensitive, then change its collation to the one that ends with ci. If you want to keep it the way it is and only make the query case-insensitive, then you can change the collation in the query. Example:
SELECT *
FROM table1
WHERE name LIKE '%BEJO%' COLLATE utf8_general_ci;
Alternatively, you can simply change the case of both sides using LOWER() or UPPER(). Example:
SELECT *
FROM table1
WHERE UPPER(name) LIKE UPPER('%BEJO%');

SQL SELECT LIKE (Insensitive casing)

I am trying to execute the sql query:
select * from table where column like '%value%';
But the data is saved as 'Value' ( V is capital ).
When I execute this query i don't get any rows.
How do i make the call such that, it looks for 'value' irrespective of the casing of the characters ?
use LOWER Function in both (column and search word(s)). Doing it so, you assure that the even if in the query is something like %VaLuE%, it wont matter
select qt.*
from query_table qt
where LOWER(column_name) LIKE LOWER('%vAlUe%');
If you want this column be case insensitive :
ALTER TABLE `schema`.`table`
CHANGE COLUMN `column` `column` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
Thus, you don't have to change your query.
And the MySQL engine will process your query quicker than using lower() function or any other tricks.
And I'm not sure that using lower function will be a good solution for index searching performance.
Either use a case-insensitive collation on your table, or force the values to be lower case, e.g.
WHERE lower(column) LIKE lower('%value%');
Try using a case insensitive collation
select * from table
where column like '%value%' collate utf8_general_ci
Use the lower() function:
select t.*
from table t
where lower(column) like '%value%';
you should use either lower or upper function to ignore the case while you are searching for some field using like.
select * from student where upper(sname) like 'S%';
OR
select * from student where lower(sname) like 'S%';
If you are using PostgreSQL, a simpler solution is to use insensitive like (ILIKE):
SELECT * FROM table WHERE column ILIKE '%value%'
I know this is a very old question, but I'm posting this for posterity:
Non-binary string comparisons (including LIKE) are case-insensitive by default in MySql:
https://dev.mysql.com/doc/refman/en/case-sensitivity.html
This will eventually do the same thing. The ILIKE works, irrespective of the casing nature
SELECT *
FROM table
WHERE column_name ILIKE "%value%"

Need MySQL idiom for checking whether string contains any of a set of characters

I need to run the following MySQL query:
SELECT id, name FROM mytable
WHERE NOT CONTAINS_ANY(name, ';.<>#$!');
except that there doesn't seem to be anything like CONTAINS_ANY (taking two string and checking whether the first string contains any character of those in the second string). What can I do instead? I would like to avoid
SELECT id, name FROM mytable
WHERE name NOT LIKE '%;%'
AND name NOT LIKE '%.%'
AND etc. etc.
and similar ugliness.
Use regex:
SELECT id, name
FROM mytable
WHERE name not rlike '[;.<>#$!]';

MySQL regex query case insensitive

In my table I have firstname and last name. Few names are upper case ( ABRAHAM ), few names are lower case (abraham), few names are character starting with ucword (Abraham).
So when i am doing the where condition using REGEXP '^[abc]', I am not getting proper records. How to change the names to lower case and use SELECT QUERY.
SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abc]';
This is my query, works fine if the records are lower case, but my records are intermediate ,my all cus name are not lower case , all the names are like ucword.
So for this above query am not getting proper records display.
I think you should query your database making sure that the names are lowered, suppose that name is the name you whish to find out, and in your application you've lowered it like 'abraham', now your query should be like this:
SELECT * FROM `test_tbl` WHERE LOWER(cus_name) = name
Since i dont know what language you use, I've just placed name, but make sure that this is lowered and you should retrieve Abraham, ABRAHAM or any variation of the name!
Hepe it helps!
Have you tried:
SELECT * FROM `test_tbl` WHERE LOWER(cus_name) REGEXP '^[abc]';
I don't know since when, but nowadays MySql REGEXP is case insensitive.
https://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
You don't need regexp to search for names starting with a specific string or character.
SELECT * FROM `test_tbl` WHERE cus_name LIKE 'abc%' ;
% is wildcard char. The search is case insensitive unless you set the binary attribute for column cus_name or you use the binary operator
SELECT * FROM `test_tbl` WHERE BINARY cus_name LIKE 'abc%' ;
A few valid options already presented, but here's one more with just regex:
SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abcABC]';

How can I search (case-insensitive) in a column using LIKE wildcard?

I looked around some and didn't find what I was after so here goes.
SELECT * FROM trees WHERE trees.`title` LIKE '%elm%'
This works fine, but not if the tree is named Elm or ELM etc...
How do I make SQL case insensitive for this wild-card search?
I'm using MySQL 5 and Apache.
I've always solved this using lower:
SELECT * FROM trees WHERE LOWER( trees.title ) LIKE '%elm%'
SELECT *
FROM trees
WHERE trees.`title` COLLATE UTF8_GENERAL_CI LIKE '%elm%'
Actually, if you add COLLATE UTF8_GENERAL_CI to your column's definition, you can just omit all these tricks: it will work automatically.
ALTER TABLE trees
MODIFY COLUMN title VARCHAR(…) CHARACTER
SET UTF8 COLLATE UTF8_GENERAL_CI.
This will also rebuild any indexes on this column so that they could be used for the queries without leading '%'
The case sensitivity is defined in the columns / tables / database collation settings. You can do the query under a specific collation in the following way:
SELECT *
FROM trees
WHERE trees.`title` LIKE '%elm%' COLLATE utf8_general_ci
for instance.
(Replace utf8_general_ci with whatever collation you find useful). The _ci stands for case insensitive.
This is the example of a simple LIKE query:
SELECT * FROM <table> WHERE <key> LIKE '%<searchpattern>%'
Now, case-insensitive using LOWER() func:
SELECT * FROM <table> WHERE LOWER(<key>) LIKE LOWER('%<searchpattern>%')
Simply use :
"SELECT * FROM `trees` WHERE LOWER(trees.`title`) LIKE '%elm%'";
Or Use
"SELECT * FROM `trees` WHERE LCASE(trees.`title`) LIKE '%elm%'";
Both functions works same
I'm doing something like that.
Getting the values in lowercase and MySQL does the rest
$string = $_GET['string'];
mysqli_query($con,"SELECT *
FROM table_name
WHERE LOWER(column_name)
LIKE LOWER('%$string%')");
And For MySQL PDO Alternative:
$string = $_GET['string'];
$q = "SELECT *
FROM table_name
WHERE LOWER(column_name)
LIKE LOWER(?);";
$query = $dbConnection->prepare($q);
$query->bindValue(1, "%$string%", PDO::PARAM_STR);
$query->execute();
use ILIKE
SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
it worked for me !!
Non-binary string comparisons (including LIKE) are case insensitive by default in MySql:
https://dev.mysql.com/doc/refman/en/case-sensitivity.html
I think this query will do a case insensitive search:
SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
You don't need to ALTER any table. Just use the following queries, prior to the actual SELECT query that you want to use the wildcard:
set names `utf8`;
SET COLLATION_CONNECTION=utf8_general_ci;
SET CHARACTER_SET_CLIENT=utf8;
SET CHARACTER_SET_RESULTS=utf8;
well in mysql 5.5 , like operator is insensitive...so if your vale is elm or ELM or Elm or eLM or any other , and you use like '%elm%' , it will list all the matching values.
I cant say about earlier versions of mysql.
If you go in Oracle , like work as case-sensitive , so if you type like '%elm%' , it will go only for this and ignore uppercases..
Strange , but this is how it is :)
SELECT name
FROM gallery
WHERE CONVERT(name USING utf8) LIKE _utf8 '%$q%'
GROUP BY name COLLATE utf8_general_ci LIMIT 5
You must set up proper encoding and collation for your tables.
Table encoding must reflect the actual data encoding. What is your data encoding?
To see table encoding, you can run a query SHOW CREATE TABLE tablename
When I want to develop insensitive case searchs, I always convert every string to lower case before do comparasion
I've always solved like this:
SELECT * FROM trees WHERE LOWER( trees.title ) LIKE LOWER('%elm%');
For example if you want to search name like Raja not raja, Royal not royal etc, add BINARY before column name in WHERE clause.
SELECT name FROM person_tbl
WHERE BINARY name LIKE "R%";