is where clause not case-sensitive??
i tried using different versions of query for the table data:
username(TEXT) pass(TEXT)
admin admin
The above table has only one entry
SELECT pass FROM lbdb_user WHERE username = "Admin";
SELECT pass FROM lbdb_user WHERE username = "admiN";
SELECT pass FROM lbdb_user WHERE username = "admin ";
all the above queries that i ran produced the same result.
Mysql collate, searching case sensitive
Usually our mysql queries are not case sensitive. In order to query case sensitive, you can use the mysql COLLATE clause.
The collate clause lets you specify a collation, which basically is a set of rules for comparing characters in a given character set.
The suffixes ci, cs, bin of a collation stand for case insensitive, case sensitive and binary, respectively. A binary collation such as utf8_bin is case sensitive as well since it compares the characters based on their numeric values.
SELECT * FROM users WHERE name like 'cRaZy' COLLATE utf8_bin;
Related
This is an interesting challenge question as there can be multiple ways to solve this problem :)
I have an ID column that is unique but case-sensitive, ex:
----- ID ------
0018000001K6dkh -> record 1
0018000001K6dkH -> record 2 (different from record 1)
As MySQL is case insensitive in utf8, it is considering the two ID values as identical:
SELECT COUNT(*) FROM table WHERE id='0018000001K6dkh' //returns 2 instead of 1
SELECT COUNT(*) FROM table WHERE id='0018000001K6dkH' //returns 2 instead of 1
I need to build a MySQL function that I can apply to this column to transform all IDs into a case-insensitive and unique IDs, ex:
function('0018000001K6dkh') = something
function('0018000001K6dkH') = something different
function('0018000000ttzlB') = something even different
function('0018000000tTzlB') = something even more different
Note: I don't want to use collations, because I would have to change all the WHEREs and JOINs in my application (Laravel PHP). I want to change the IDs permanently.
According the official MySQL documentation you can set the collation on a per column basis (*see data_type section for CHAR, VARCHAR, TEXT, ENUM, and SET types).
This should change the default manner in which they are compared to string literals and each other, but I am not familiar with the rules used when comparing fields of differing collation with each other.
This is from mysql http://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html
To cause a case-sensitive comparison of nonbinary strings to be case
insensitive, use COLLATE to name a case-insensitive collation. The
strings in the following example normally are case sensitive, but
COLLATE changes the comparison to be case insensitive:
SET #s1 = 'MySQL' COLLATE latin1_bin;
SET #s2 = 'mysql' COLLATE latin1_bin;
SELECT #s1 = #s2;
return false
using #s1 COLLATE latin1_swedish_ci = #s2;
SELECT #s1 COLLATE latin1_swedish_ci = #s2;
return true
or use BINARY
SELECT COUNT(*) FROM table WHERE BINARY id='0018000001K6dkh'
SELECT COUNT(*) FROM table WHERE BINARY id='0018000001K6dkH'
I want to search for a name on database. But I just want select Bill , not biLL or BiLL or ... just "Bill". But when I use this query which shows Bill , BiLL, BILL, bilL and ...
query=`select * from names where name='Bill'`
To quote the documentation:
The default character set and collation are latin1 and latin1_swedish_ci, so nonbinary string comparisons are case insensitive by default. This means that if you search with col_name LIKE 'a%', you get all column values that start with A or a. To make this search case sensitive, make sure that one of the operands has a case sensitive or binary collation. For example, if you are comparing a column and a string that both have the latin1 character set, you can use the COLLATE operator to cause either operand to have the latin1_general_cs or latin1_bin collation
You can overcome this by explicitly using a case sensitive collation:
select * from names where name='Bill' COLLATE latin1_general_cs
There is also another solution to set the collection of the column to utf8mb4_unicode_520 or any case sensitive standard collections.
MySQL table is recognizing cód and cod as same. How can i avoid this problem.
The table is COLLATE='utf8_general_ci'
You should look for the characters set, tablename, and column names of the fields you want to use
Make sure they are set to:
COLLATE='utf8_bin'
Make sure you don't put the _ci at the end as it stands for case insensitive
If you can't get to the table and db in the database, you can use it on your queries:
SELECT * FROM tablex WHERE LOWER(column) = 'cód' COLLATE utf8_bin
This question already has answers here:
What does character set and collation mean exactly?
(4 answers)
Closed 8 years ago.
1/ Sometime you write
ALTER DATABASE [MyDb] COLLATE SQL_Latin1_General_CP1_CI_AS
after a moment, if you try
ALTER DATABASE [MyDb] COLLATE FRENCH_CI_AS
it may not succeed. why this?
2/ if table1 is SQL_Latin1_General_CP1_CI_AS and if table2 is FRENCH_CI_AS
one cannot write ...where table1.field1= table2.field2
What is the consequence of COLLATE in a database or table ?
you can compare your query with collate name
table1.field1 = table2.field2 COLLATE FRENCH_CI_AS
otherwise Alter Column.(If you want to change field COLLATE)
ALTER TABLE [dbo].[TableName]
ALTER COLUMN [FieldName] [DataType] COLLATE [Collate Name] NOT NULL
This answer is based on experience from working with Ms Sql Server. I have only a little experience from working with other databases, but I imagine much of this would apply for them as well.
From Msdn (here):
Collations let users sort and compare strings according to their own
conventions
Collations are used to let sql server know which characters are considered the same. If a case insensitive collation is used, X and x are considered the same. If a case sensitive collation is used, they are different. In an accent insensitive collation e and é might be considered the same, while in an accent sensitive collation they are different.
At least with Ms Sql Server, if you are comparing two strings that are stored in different collations, you must tell the server which collation to use in the comparison by using the COLLATE clause already mentioned in other answers. Not sure about how this is done in other databases.
If your table contains a row with a column that contains the text ÖBc and you select from this table like so:
select COL from TBL where COL = 'obc'
Then the row will be found using a case and accent insensitive collation like latin1_general_ci_ai. Accent insensitivity means that O and Ö are the same, and case insensitivity takes care of the case mismatch.
With the same select and collation latin1_general_cs_ai the row will not be found because of case sensitivity.
Similarly with collation latin1_general_ci_as, the row will not be found because of accent sensitivity.
Collation is also used to determine alphabetical sort order when sorting results. This determines if upper and lower case charachers are sorted differently and also where accented charaters are sorted.
You can compare item with different collations. For SQL Server you use :
table1.field1 = table2.field2 COLLATE FRENCH_CI_AS
or
table1.field1 = table2.field2 COLLATE SQL_Latin1_General_CP1_CI_AS
(or whatever the field1 collation is).
Note: You should try and leave the collation as the server default, unless you have a specific reason to do otherwise.
I have got a problem with a simple Select Query and special chars. I want to select the name Änne.
SELECT * FROM `names` WHERE `name` = 'Änne'
utf8_general_ci
Änne
Anne
okay, ...
utf8 general ci is a very simple collation. What it does it just
removes all accents then converts to upper case and uses the code of this sort of "base letter" result letter to compare.
http://forums.mysql.com/read.php?103,187048,188748
utf8_unicode_ci
Änne
Anne
why?
utf8_bin
Änne
utf8_bin seems to be the right choice at this point, but i have to do my search case insensitiv.
SELECT * FROM `names` WHERE `name` = 'änne'
utf8_bin
none
Is there no way to do so?
I could use php ucwords() to uppercase the first letters, but i would prefer to find a DB solution.
edit: ucwords('änne') = änne, so i cant use that too
SELECT * FROM `names` WHERE lower(`name`) = 'änne'
is working for me, because i don't have a difference between 'Änne' and 'änne' in my DB.
what about:
SELECT * FROM `names` WHERE upper(`name`) = upper("änne")
Quoting doc:
The default character set and collation are latin1 and
latin1_swedish_ci, so nonbinary string comparisons are case
insensitive by default. This means that if you search with col_name
LIKE 'a%', you get all column values that start with A or a. To make
this search case sensitive, make sure that one of the operands has a
case sensitive or binary collation
That means that case sensitive results are because you have set a binary collation. You can set collation column to utf8_general_ci and change it on searchs:
col_name COLLATE latin1_general_cs LIKE 'a%'
There is an error in your MySQL code:
SELECT * FROM names WHERE name = "Änne"
Remove the quotes around the table name and the field name.