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

sql检索问题
数据表:
id       name       title       body       q_time   a_time   isJT
1           --           注册         ---           ---         ---         0
2           --           注册         ---           ---         ---         0
3           --           注册         ---           ---         ---         1
分别检索出isJT为0,1的
我的存储过程:
create   proc   pro_search
@q_title   nvarchar(200),
@q_body   varchar(8000)
as   select   *   from   question   where   (isJT= '1 'and   q_title   like   '% '+   @q_title   + '% '   or   q_body   like   '% '+   @q_body   + '% ')  
order   by   q_time   asc
order   by   q_time   asc
--exec   pro_search   '注册 ', '注册 '
读出来的结果isJT=0的也会读出来,
改成这样还是一样
create   proc   pro_search
@q_title   nvarchar(200),
@q_body   varchar(8000),
@isJT   int
as   select   *   from   question   where   (q_title   like   '% '+   @q_title   + '% '   or   q_body   like   '% '+   @q_body   + '% ')  
order   by   q_time   asc
exec   pro_search   '注册 ', '注册 ', '1 '
结果还是一样,isJT为0的也能读出来,帮我解决一下好吗?谢谢!

------解决方案--------------------
修改下:
select * from question where (isJT= '1 'and q_title like '% '+ @q_title + '% ' or q_body like '% '+ @q_body + '% ' and isJT= '1 ')

------解决方案--------------------
and的优先级比or的大。。
select * from question where (isJT= '1 'and (q_title like '% '+ @q_title + '% ' or q_body like '% '+ @q_body + '% ' ))


as select * from question where ((q_title like '% '+ @q_title + '% ' or q_body like '% '+ @q_body + '% ') and isJT=@isJT)