ORACLE的instr函数,想了一下午,求大神解惑,不胜感激!!!
问题描述:
有两个表:
1、pm_ci
2、pm_stu
当我执行如下sql的时候,结果如图:
SQL语句:
select a.ci_id,instr(replace(a.stu_ids,',',''),b.stu_id)
from pm_ci a,pm_stu b
where a.ci_id=2
and instr(replace(a.stu_ids,',',''),b.stu_id)>0
结果为:
符合我预期的结果。
但是加了一句 条件之后,我迷惘了
SQL语句:
select a.ci_id,instr(replace(a.stu_ids,',',''),b.stu_id)
from pm_ci a,pm_stu b
where a.ci_id=2
and instr(replace(a.stu_ids,',',''),b.stu_id)>0
and instr(replace(a.stu_ids,',',''),b.stu_id)= b.stu_id
结果:
为何只返回1。。。。
2被谁吃了。。。
答
第一次返回的stu_name为张三,赵六,他们的stuid为1和4,在stu_ids中的位置为1和2
第二次instr(replace(a.stu_ids,',',''),b.stu_id) 的结果为1和2,和stuid求交集
只有stuid为1才符合条件