如何在MySQL中搜索确切的字符串值?

时间:2016-09-30 12:47:58

标签: mysql sql character-encoding

我收到了这段代码:

select * from locality WHERE name ="ISTASYON"

这与mySQL完美配合。问题来了,当我尝试执行但结果不正确时

结果代码:

28BAF9346A41E4E4E0501AAC4524363B    0       iSTASYON
402881a4523b52d201523b6c2afb4166    0       İSTASYON
402881a4523b52d201523b6c38b7417c    0       İSTASYON
402881a4523b52d201523baa9faf0092    0       İSTASYON
402881a4523b52d201523baab059009f    0       İSTASYON
402881a4523b52d201523baad01a00b7    0       İSTASYON
58441bc4c054447ebe1cddbfeef958b5    0       ISTASYON
fa7fb88d1d4c41feb497b08f42066c82    1   2016-04-19 09:53:41.000000  İSTASYON

我的问题是结果包含ISTASYON,İSTASYON,ıSTASYON,iSTASYON但我想只有ISTASYON

如何解决这个问题?

5 个答案:

答案 0 :(得分:1)

您可以使用COLLATEBINARY运算符强制进行二进制比较:

SELECT * FROM locality WHERE name COLLATE utf8_bin = "ISTASYON"

SELECT * FROM locality WHERE BINARY name = "ISTASYON"

如果希望始终以这种方式处理列,请使用二进制排序规则对其进行声明 有关详细信息,请参阅docs

答案 1 :(得分:1)

你可以像这样使用select查询,我也使用它,这很有帮助。

Select * from `users` where username COLLATE latin1_general_cs LIKE '%$email%'

答案 2 :(得分:0)

默认字符集和排序规则是latin1和latin1_swedish_ci,因此非二进制字符串比较默认情况下不区分大小写。这意味着如果使用col_name LIKE'i%'进行搜索,则会获得以I或i开头的所有列值。要使此搜索区分大小写,请确保其中一个操作数具有区分大小写或二进制排序规则。

col_name COLLATE latin1_general_cs LIKE 'i%'
col_name LIKE 'i%' COLLATE latin1_general_cs
col_name COLLATE latin1_bin LIKE 'i%'
col_name LIKE 'i%' COLLATE latin1_bin

答案 3 :(得分:0)

从地区选择* WHERE BINARY name =" ISTASYON"

从地区选择* WHERE name = BINARY" ISTASYON"

答案 4 :(得分:0)

计划A:这将区分大小写且区分重音:

将列设为utf8_bin(或utf8mb4_binlatin1(和latin1_bin)将

设置

ALTER TABLE tbl MODIFY COLUMN ... COLLATION utf8_bin ...;

注意:如果您还想在其他情况下使用相同的列进行大小写折叠和/或重点剥离,这将会对这些情况造成伤害。

计划B:这解决了无点ı和其他一些土耳其特质。

COLLATION utf8_turkish_ci(或utf8mb4_turkish_ci)将这些等同于:I=ı,然后将以下内容视为不同,稍后,但均等于:i=Ì=Í=Î=Ï=ì=í=î=ï=Ī=ī=Į=į=İÜ=ü U之后V之前Ğ=ğ。同样GH之后和public String encrypt(String orig, String iv, String key) { int i, j, result; String cipherText = ""; int b = iv.length(); //loops through the entire set of chars for (i = 0; i < text.length; i += b) { //Splits the char into block the size of the IV block. for (j = 0; j < b; j++) { //checks to for first block. If so, begains with iv. if (i == 0) { //adding the iv to the block chars char one = text[j], two = iv.charAt(j); result = (((iv.charAt(j) - 'a') + (text[j] - 'a')) % 26); //prints out test result. System.out.println(one + " + " + (iv.charAt(j) - 'a') + "= " + result); } else { //block chainging, addition, with new key. result = ((key.charAt(j) - 'a') + (text[j + i] - 'a')) % 26; // System.out.println(result); } } } return cipherText; } 之前。有关详细信息,请参阅utf8 collations