2009-09-01 99 views
0
Will the following query evaluate to true (1), false (0), or NULL? 

SELECT '%' LIKE ' % '; 

提供的答案是MySQL的比较和 '%'

The '%' character is matched by '%', but not by the space characters surrounding it, so the expression evaluates to false. 

+----------------+ 
| '%' LIKE ' % ' | 
+----------------+ 
|   0 | 
+----------------+ 

但我以为%可匹配零以上字符?所以%可以匹配%+ Spaces?或者字符是否包含通配符?

UPDATE:

哦,但如果发生了比较其他方式ARND这是真的...嗯...

SELECT ' % ' LIKE '%';
Any non-NULL string is matched by the '%' metacharacter, so the expression evaluates to true. 

+----------------+ 
| ' % ' LIKE '%' | 
+----------------+ 
|   1 | 
+----------------+ 

回答

2

逻辑是错误的。你不得不写

select ' % ' like '%' 

如果你正在写像“%”,这意味着在第一个字符串必须是空间,那么任何符号,最后一个更大的空间。通配符用于类似声明,在第一个字符串中不是通配符,而是符号。

0

不能完全确定你的问题是什么,但如时间:

样品表,mytbl

col1 
---- 
abc 
def 
feh 
zba 
a b 

Query1 
------ 
select * from mytbl where col1 like '%b%' 

Result1 
------- 
abc 
zba 
a b 

Query2 
------ 
select * from mytbl where col1 like '%b' 

Result2 
------ 
a b 

Query3 
------ 
select * from mytbl where col1 like 'a%' 

Result3 
------- 
abc 
a b 

Query4 
------ 
select * from mytbl where col1 like '% b%' 

Result4 
------- 
a b 

Query5 
------ 
select * from mytbl where col1 like '% b %' 

Result5 
------- 
null 

正如你所看到的,%匹配零个或多个字符。非%字符被视为文字。这意味着% b %正在寻找anything + space + b + space + anything

希望这会有所帮助。