2009-09-01 3 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와 일치 할 수 있습니까? 또는 문자 nt는 와일드 카드를 포함합니까?

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 

당신이 볼 수 있듯이, % 일치하는 0 개 이상의 문자. Non-% 문자는 리터럴로 처리됩니다. 즉, % b %anything + space + b + space + anything을 찾고 있습니다.

이 정보가 도움이되기를 바랍니다.