2016-07-05 46 views
0

我有一個示例表ExampleTableNameCodeVARCHAR):MySQL的使用 '喜歡' 和 'IN' 一起

Name  Code 
test1 2016/554 
test2 2016/13554 
test3 2015/9893 
test4 2015/74584 

,我在Visual Studio中的工作查詢:

select * from Example_Table where code LIKE '%' + '2016' + '%' 

但我希望該用戶可以定義包含部分代碼的字符串。這些部分由','分隔。

例如:string = 2016,745我想要得到test1,test2,test4

select * from Example_Table where code LIKE IN (string with elements) 
+1

這是MySQL或SQL服務器?請適當標記。 –

+0

這是mySQL。 – Pepe

回答

2
select * from [ExampleTable] where code LIKE '%2016%' or code LIKE '%745%'; 

這將做的工作,並得到的答案爲test1,test2,test4

使用OR運算符。

REFFERENCE http://dev.mysql.com/doc/refman/5.7/en/logical-operators.html

+0

但我不知道字符串中有多少項目。我怎樣才能做到動態? – Pepe

+0

統計字符串,然後使用OR運算符並連接到sql命令。然後執行它。 – Aseem

+0

啊好吧謝謝! – Pepe

0

... WHERE代碼= '%STR1%' 或代碼= '%STR2賽車%' 或代碼= '%STR3' ......

+0

你會得到不好的表現與此OR操作 –