2017-09-25 93 views
1

我在mysql的表中有一個名爲type的列。含量低於在where子句中使用子字符串的正確方法mysql查詢

Type 
Test 12 
Test abc 
start 1 
start abcd 
end 123 

現在我想選擇記錄,其中type開始與Test

預期結果:

Test 12 
Test abc 

但我正在逐漸要麼

測試12 或空的結果

我試圖象下面這樣:

select * from table where type = 'Test 12' 
select * from table where type = '%Test%' 
select * from table where type = '%Test' 

什麼應該是正確sql聲明。

回答

1

使用like關鍵字

select * from table where type LIKE 'Test%' 
2

如果你想要做的部分匹配您需要的LIKE操作:

SELECT * FROM table WHERE type LIKE 'Test%' 
+0

%測試** **結束與測試,而不是與它開始。 –

+0

@JacquesAmar更正。 – tadman

1

的平等(=)運算符不接受外卡。您應該使用like運營商代替:

SELECT * FROM table WHERE type LIKE 'Test%' 
1

Sooo close!你想以字符串開始,則不要在一開始使用%,並使用LIKE代替=

select * from table where type LIKE 'Test%'