日期:2014-05-18  浏览次数:20589 次

SQL select (查询字符串)初级问题请教
某字段[class], 字符型, 内容形式为 39,58,61

想求出所有本字段中含有58的所有记录.... 查不出来..不知道哪错啦?

SELECT Id,Title From NewsInfo Where class in ('58') Order By Id DESC

------解决方案--------------------
SQL code
SELECT Id,Title From NewsInfo Where class like '%58%' Order By Id DESC

------解决方案--------------------
SQL code
WHERE CHARINDEX(',58,',','+class+',')>0

------解决方案--------------------
SQL code
SELECT Id,Title From NewsInfo Where class like '%58%' Order By Id DESC

------解决方案--------------------
SQL code
SELECT Id,Title From NewsInfo Where class like '%58%' Order By Id DESC

------解决方案--------------------
SQL code
SELECT Id,Title From NewsInfo Where class like '%58%' Order By Id DESC

------解决方案--------------------
SQL code
SELECT Id,Title From NewsInfo Where charindex('58,',class+',')>0  Order By Id DESC

------解决方案--------------------
SQL code
SELECT Id,Title From NewsInfo Where ',' + class + ',' like '%,58,%' Order By Id DESC
SELECT Id,Title From NewsInfo Where charindex(',58,' , ',' + class + ',') > 0 Order By Id DESC

------解决方案--------------------
探讨
SQL codeSELECT Id,Title From NewsInfo Where ',' + class + ',' like '%,58,%' Order By Id DESC
SELECT Id,Title From NewsInfo Where charindex(',58,' , ',' + class + ',') > 0 Order By Id DESC

------解决方案--------------------
探讨
某字段[class], 字符型, 内容形式为
39,58,61
50,28
66
83,88,89

想求出所有本字段中含有58的所有记录.... 查不出来..不知道哪错啦?

SELECT Id,Title From NewsInfo Where class in ('58') Order By Id DESC

------解决方案--------------------
SQL code
declare @t table(a varchar(300))
insert @t select '39,58,61' 
insert @t select '50,28' 
insert @t select '66' 
insert @t select '83,88,89'

select * from @t where a like '%58%'

a                     
---------------------

(所影响的行数为 2 行)

------解决方案--------------------
SQL code

SELECT Id,Title From NewsInfo Where class like '%58%' Order By Id DESC

------解决方案--------------------
1、方法一
SQL code
select id,title from newsinfo wher class like '%58%' order by id desc