如果没有EXISTS不适用于尾随空格

时间:2014-06-05 19:36:11

标签: mysql sql subquery

我正在运行命令SELECT distinct winner FROM history WHERE NOT EXISTS (SELECT * FROM players WHERE players.name = history.winner)

如果history.winner类似于“测试”(通知尾随空格)并且players.name已经具有“测试”,则似乎失败

我对于不区分大小写有同样的问题所以我将两个字段整理都设为latin1_general_cs,如果它起任何作用。我认为解决方案可能会导致使用BINARY关键字但执行

SELECT distinct BINARY winner FROM history WHERE NOT EXISTS (SELECT * FROM players WHERE BINARY players.name = history.winner)使查询非常慢(速度太慢,我在1分钟后退出)。

2 个答案:

答案 0 :(得分:1)

您可以使用Trim()功能来完成此任务:

SELECT DISTINCT h.winner 
FROM history h
WHERE NOT EXISTS 
(
  SELECT * 
  FROM players p  
  WHERE Trim(p.name) = Trim(h.winner)
)

答案 1 :(得分:0)

mysql TRIM()函数将从特定字段中删除前导和尾随空格。

SELECT distinct winner FROM history WHERE NOT EXISTS (SELECT * FROM players WHERE players.name = TRIM(history.winner))

如果您只想修剪尾随空格,可以改用RTRIM()

Relevant Documentation