I have a query like this :
SELECT * FROM category_keyword WHERE keyword = 'cho'
This query is return result keyword ={ cho , chợ , chờ , chợ ...}. I only want the result is keyword = 'cho' ( not 'chợ, chờ ...') . How can I do?
The collation of table is utf8_unicode_ci
With utf8 collation, cho does equal chờ. If you want to compare as binary:
SELECT * FROM category_keyword WHERE keyword = CONVERT('cho' USING binary)
Change the collation for the column to utf8_bin. This is better than using CONVERT because it allows the use of an index. However, if you sometimes need utf8_bin (exact match) and sometimes need utf8_unicode_ci (for case folding and accent stripping), you are out of luck, performance-wise.
Related
Duh right off the bat you'd think, "use ORDER BY and then the column" but I have values of:
A
Z
B
a
z
And when I sort them using this query:
SELECT * FROM Diaries ORDER BY title ASC;
I then get this:
A
B
Z
a
z
When I want to get something like this, first issue:
A
a
B
Z
z
I had the same sorting issue else where, second issue, but I was able to fix it with this: By temporarily putting all characters in lowercase
for (NSString *key in [dicGroupedStories allKeys]) {
[dicGroupedStories setValue: [[dicGroupedStories objectForKey: key] sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSString *stringA = [[a objectStory_title] lowercaseString];
NSString *stringB = [[b objectStory_title] lowercaseString];
return [stringA compare: stringB];
}] forKey: key];
}
Only reason why I don't use this Comparator to sort my first issue is bc I don't want to execute my query then sort them then use the array.
Question: I want to know if there's a way to sort them how I want, like I did in my second issue, in a SQL query
objects id a and id b are arrays that contain other objects like title, date created, description, etc. objectDiary_title returns a NSString
In SQL, you can use the lower() or upper() functions in the order by:
ORDER BY lower(Diaries), diaries
You can use COLLATE with xxx_ci where ci means case insensitive. For example:
SELECT * FROM Diaries ORDER BY title COLLATE 'latin1_general_ci' ASC;
There's more information regarding case sensitivity in MySQL here: https://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html. It's useful for doing searches and comparisons as well.
Use a case-insensitive collation, such as:
ORDER BY Diaries COLLATE utf8_unicode_ci ;
However, changing collation on-the-fly, like any convertion on-the-fly, makes the query unable to use an index (which is acceptable if the data set to be sorted is small enough).
If performance is an issue then you had better reindex the column with the target collation:
ALTER TABLE MODIFY COLUMN Diaries VARCHAR(10) COLLATE utf8_unicode_ci ;
ORDR BY will then be case insensitive by defaut and can use an index on this column.
utf8_unicode_ci is just an example. Just make sure you use a collation *_ci (for Case-Insensitive) which is compatible with the column's encoding
I have the following query:
SELECT *
FROM (
`teams`
)
WHERE `name` = 'mi equiñerolico'
And the result of this query is:
idteam|name|datet
9|mi equiñerolíco|2012-06-23 12:15:32
As you can see it retrieves a row with the name 'mi equiñerolíco' even though that my Where clause establish that it must be 'mi equiñerolico'.
teams table has utf8_general_ci collation.
How can I solve this ambiguity?
You could use a binary collation to force an accent-sensitve compare:
select *
from teams
where name = 'mi equiñerolico' collate utf8_bin
Is it possible to force case-sensitive for a query?
Mine sounds like this:
"SELECT g_path FROM glyphs WHERE g_glyph = :g_glyph ORDER BY rand()"
if g_glyph = r, the result can be R or r and it's not what I expect.
I'm looking for a case-sensitive return.
I googled my issue and I found this solution:
/*Case-sensitive sort in descending order.
In this query, ProductName is sorted in
case-sensitive descending order.
*/
SELECT ProductID, ProductName, UnitsInStock
FROM products
ORDER BY BINARY ProductName DESC;
But the following line doesn't work at all:
"SELECT g_path FROM glyphs WHERE g_glyph = :g_glyph ORDER BY BINARY rand()"
Any Suggestion?
Thank you very much for your help.
The order and equality of characters is defined by the collation. In most cases, a case-insensitive collation is used.
If you need to use a strict, case-sensitive comparison for a specific datum, use the BINARY operator:
mysql> SELECT 'a' = 'A';
-> 1
mysql> SELECT BINARY 'a' = 'A';
-> 0
mysql> SELECT 'a' = 'a ';
-> 1
mysql> SELECT BINARY 'a' = 'a ';
-> 0
So in your case:
SELECT g_path FROM glyphs WHERE BINARY g_glyph = :g_glyph ORDER BY rand()
This is covered in the manual page Case Sensitivity in String Searches.
You need to specify a case sensitive or binary collation.
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:
col_name COLLATE latin1_general_cs LIKE 'a%'
col_name LIKE 'a%' COLLATE latin1_general_cs
col_name COLLATE latin1_bin LIKE 'a%'
col_name LIKE 'a%' COLLATE latin1_bin
If you want a column always to be treated in case-sensitive fashion, declare it with a case sensitive or binary collation. See Section 13.1.14, “CREATE TABLE Syntax”.
The _cs in the collation name stands for "case sensitive".
My Table collation is "utf8_general_ci". If i run a query like:
SELECT * FROM mytable WHERE myfield = "FÖÖ"
i get results where:
... myfield = "FÖÖ"
... myfield = "FOO"
is this the default for "utf8_general_ci"?
What collation should i use to only get records where myfield = "FÖÖ"?
SELECT * FROM table WHERE some_field LIKE ('%ö%' COLLATE utf8_bin)
A list of the collations offered by MySQL for Unicode character sets can be found here:
http://dev.mysql.com/doc/refman/5.0/en/charset-unicode-sets.html
If you want to go all-out and require strings to be absolutely identical in order to test as equal, you can use utf8_bin (the binary collation). Otherwise, you may need to do some experimentation with the different collations on offer.
For scandinavian letters you can use utf8_swedish_ci fir example.
Here is the character grouping for utf8_swedish_ci. It shows which characters are interpreted as the same.
http://collation-charts.org/mysql60/mysql604.utf8_swedish_ci.html
Here's the directory listing for other collations. I'm no sure which is the used utf8_general_ci though. http://collation-charts.org/mysql60/
How do I search lower case in MySQL?
I have the following
$sql = "SELECT * FROM TABLE_NAME WHERE column LIKE '%$search%'";
How do I make sure the values in "column" are matched to lowercase?
You should either set your column's collation to a case-sensitive one, like UTF8_BIN, or make an additional check in the filter:
SELECT *
FROM table_name
WHERE column LIKE '%$search$%'
AND column COLLATE UTF8_BIN = LOWER(column COLLATE UTF8_BIN)
Note that if your collation is case-insensitive, like UTF8_GENERAL_CI, then the following query:
SELECT LOWER('A') = ('A')
will return true. You should explicitly coerce your column to a case-insensitive collation before doing a comparison.
Assuming I'm interpreting correctly, use the MySQL LOWER() function:
$sql = "SELECT * FROM TABLE_NAME WHERE LOWER(column) LIKE '%$search%'";
This will convert everything in column to lowercase before testing it against the search value.