我有一张 table
studentId Classes
1 Auto CAD,Electronics
2 Instrumentation,Chemical,IT,English
3 Computer,Math,Hindi
4 Physics,Accountancy
5 Bengali,Geography
现在我想使用单个字符串“Instrumentation,Geography,Physics”进行搜索
它将显示以下内容
studentId Classes
2 Instrumentation,Chemical,IT,English
4 Physics,Accountancy
5 Bengali,Geography
因为studentid 2包含Instrumentation,studentid 4包含Physics,studentid 5包含Geography
如何使用 SELECT
语句来完成此操作?不像“%Instrumentation%”这样的类
或像“%Geography%”或像“%Physics%”这样的类,因为我想使用单个变量来字符串“Instrumentation,Geography,Physics”或用逗号分隔的字符串的任何组合。
最佳答案
试试这个:
SELECT *
FROM your_Table
WHERE ','+Classes+',' like '%,Instrumentation,%'
OR ','+Classes+',' like '%,Geography,%'
OR ','+Classes+',' like '%,Physics,%'
我在列和搜索条件中添加了一个逗号,以便它只返回以逗号结尾的字符串
关于Sql Server 在另一个字符串中查找部分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12384334/