2009-09-01 23 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 | 
+----------------+ 

ですが、私は%がゼロ以上文字を一致させることができます思いましたか?だから%は%+スペースにマッチすることができますか?または文字にワイルドカードが含まれていますか?

UPDATE:

ああが、比較は、他の方法は、それが真実であるarnd起これば...うーん...

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

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

答えて

2

ロジックが間違っています。あなたは「%」のように記述している場合

select ' % ' like '%' 

を書かなければならなかった、それは最初の文字列にスペース、その後、任意のシンボルと最後の1つの以上のスペースでなければならないことを意味しています。ワイルドカードは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 

%は0個以上の文字に一致します。非%文字はリテラルとして扱われます。つまり、% b %anything + space + b + space + anythingを探しています。

うまくいけば、これが役に立ちます。