我有两张桌子:
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
来自
words
与una
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
,结果似乎没问题但是如果不使用相同的情况则失败(例如:Uñ
代替uñ
)
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
用于数据库,表格和列。
知道如何解决这个问题吗?
答案 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);