当前位置: 代码迷 >> Sql Server >> 一个Select语名句怎么写
  详细解决方案

一个Select语名句怎么写

热度:251   发布时间:2016-04-27 21:56:59.0
一个Select语名句如何写。
如何取出数据库中这样的记录
图书名,图书所属专题book_ztid:形如
书名A           4
书名B           2,4,5
书名C           3,5,14,24,25,34
书名D           4,24

现在搜索专题book_ztid为4的的记录

------解决方案--------------------
select * from 表 where charindex( ',4, ',book_ztid)> 0
------解决方案--------------------
select * from tablename where charindex( '4 ',book_ztid)> 0
------解决方案--------------------
select * from 表 where charindex( ',4, ', ', '+book_ztid+ ', ')> 0

------解决方案--------------------
select * from tablename where charindex( ',4, ', ', '+book_ztid+ ', ')> 0

------解决方案--------------------
create table T(book_name nvarchar(10), book_ztid varchar(20))
insert T select '书名A ', '4 '
union all select '书名B ', '2,4,5 '
union all select '书名C ', '3,5,14,24,25,34 '
union all select '书名D ', '4,24 '

select * from T
where charindex( ',4, ', ', '+book_ztid+ ', ')> 0

--result
book_name book_ztid
---------- --------------------
书名A 4
书名B 2,4,5
书名D 4,24

(3 row(s) affected)
------解决方案--------------------
select * from tablename where tablename where book_ztid like '4 ' or book_ztid like '%,4,% ' or book_ztid like '4,% ' or book_ztid like '%,4 '
------解决方案--------------------
select * from tablename where ', '+book_ztid+ ', ' like '%,4,% '
  相关解决方案