现在有一表test,字段为
name varchar(20)
re varchar(100)
其中re的格式为“统考说明|统考批次|统考科目编号”
数据wang 统考报名费|200601|1
zhang 统考报名费|200602|2
...现在选择姓名为wang 批次200601科目为2的数据,sql语句怎样写???
name varchar(20)
re varchar(100)
其中re的格式为“统考说明|统考批次|统考科目编号”
数据wang 统考报名费|200601|1
zhang 统考报名费|200602|2
...现在选择姓名为wang 批次200601科目为2的数据,sql语句怎样写???
declare @t table(name varchar(20),re varchar(100))
insert @t select 'wang','统考报名费|200601|1'
union all select 'zhang','统考报名费|200601|2'
---------------------------
select * from @t
where name='wang' and charindex('|200601|','|'+re+'|')>0 and charindex('|1|','|'+re+'|')>0
1.re格式本来就是**|**|** 查询中'|'+re+'|'还有必要吗
2.如果我想选择统考科目编号为1的 用('|1|','|'+re+'|')>0
可是会把统考批次中含1的比如200601的错误选择出来,请问如何改进???
如inner join 统考批次表 on 统考批次表.id=test.???????(这里如何写呢?)
可是会把统考批次中含1的比如200601的错误选择出来,请问如何改进???
------------------------------------
declare @t table(name varchar(20),re varchar(100))
insert @t select 'wang','统考报名费|200601|1'
union all select 'zhang','统考报名费|200601|2'
---只查询编号为1,没问题啊!
select * from @t
where charindex('|1|','|'+re+'|')>0
有必要,确保唯一性啊!第二个问题就是一个例子,查询编号1时批次字段有1,编号字段也有1,这样需要用'|'来标识出来!
select substring(re,charindex('|200601|',re)+1,charindex('|200601|',re)) newbatchid from test
inner join exambatch where exambatch.id=test.newbatchid --关联批次表
where charindex('|1',re)>0
to我爱世界杯
1.re格式本来就是**|**|** 查询中'|'+re+'|'还有必要吗
-----------------------------------------------------------------------非常有毕要。
有表 tb
ID note
1 2,11,3
2 2,1,3,4
查出字段中note含数字1记录
SELECT .. WHERE CHARINDEX('1',note)>0 会将两条记录都查出来, 而
WHERE CHARINDEX(',1,' , ',' + note +',')则不会
还有另一种情况
note
,2,1,3
2,1,3
这个note第一条记录中,有三个字符值 ',2' '1' '3' ,同样不加','用以区别,你查不出来要的是第一条还是第二条记录当然,可能你的表中数据比较特殊不会出现上述两种情况,但为避免万一,这样做还是很有必要的。或者羊成个良好习惯。
select *,substring(g.re,charindex('|200602|',g.re)+1,charindex('|200602|','|'+g.re+'|')-1) newbatchid
from gatherorder g
inner join tb_unifiedExambatch t on t.uniexambatchid=g.newbatchid
where charindex('|1|','|'+g.re+'|')>0提示说newbatchid无效???
其实这也是个逻辑问题,先有条件,再有查询结果, 还是先有查询结果才有条件。想想就明白了。