Mysql SELECT unicode字符

时间:2017-10-31 12:27:47

标签: mysql unicode

我有两张桌子:

words表:

+----+-------+------+
| id | word  | lang |
+----+-------+------+
|  1 | uña   | es   |
|  2 | two   | en   |
|  3 | three | en   |
|  4 | four  | en   |
+----+-------+------+

stop_words表:

+----+------+------+
| id | word | lang |
+----+------+------+
|  1 | una  | es   |
|  2 | one  | en   |
+----+------+------+

我需要从words表中选择一个单词,其中单词不在stop_words表中

  • 如果我尝试以下查询,结果为空,因为uña来自 wordsuna

    中的stop_words匹配
    SELECT  *
    FROM words a WHERE word LIKE  'uñ%' AND lang = 'es'
    AND NOT EXISTS( Select *
        FROM stop_words as b WHERE a.word = b.word AND lang = 'es'
    

  • 如果我尝试使用BINARY,结果似乎没问题但是如果不使用相同的情况则失败(例如:代替

    SELECT  *
    FROM words a WHERE word LIKE BINARY 'Uñ%'  AND lang = 'es'
    AND NOT EXISTS( Select *
        FROM stop_words as b WHERE BINARY a.word = BINARY b.word AND lang = 'es'
    )
    

我将utf8mb4_unicode_ci用于数据库,表格和列。

知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

SELECT id, word, lang 
FROM words 
WHERE BINARY word NOT IN (Select BINARY word FROM stop_words);

结果:

+----+-------+------+
| id | word  | lang |
+----+-------+------+
|  1 | uña   | es   |
|  2 | two   | en   |
|  3 | three | en   |
|  4 | four  | en   |
+----+-------+------+

使用utf8_unicode_ci表格

编辑: 对于同样的情况

SELECT  id, word, lang 
FROM words 
WHERE BINARY LOWER(word) NOT IN (Select BINARY LOWER(word) FROM stop_words);