2009-09-01 76 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

希望這會有所幫助。