当前位置: 代码迷 >> Sql Server >> 一个SQL查询的初学者有关问题~
  详细解决方案

一个SQL查询的初学者有关问题~

热度:86   发布时间:2016-04-27 16:16:31.0
一个SQL查询的菜鸟问题~请指教啊~~~
表名:[news]
字段:news_id,news_title,category

如果有一条数据category存放的内容为1,2,3,4,5,6
为什么下面的SQL语句查询不出数据
select   *   from   [news]   where   '1 '   in(category)   and   news_id=2


------解决方案--------------------
select * from [news] where charindex( '1 ',category) > 0 and news_id=2

------解决方案--------------------

--改用charindex
Select * from [news] where CharIndex( ',1, ', ', ' + category + ', ') > 0 and news_id=2
------解决方案--------------------
表名:[news]
字段:news_id,news_title,category

如果有一条数据category存放的内容为1,2,3,4,5,6
为什么下面的SQL语句查询不出数据
select * from [news] where '1 ' in(category) and news_id=2


select * from [news] where ',1, ' in( ', '+category+ ', ') and news_id=2

------解决方案--------------------
select * from [news] where patindex( '%,1,% ', ', '+category+ ', ')> 0 and news_id=2
  相关解决方案