select * from A where A.code like (select code from B where pcode='111')||'% 红字部分查询的code结果不止一个吧
select * from A where A.code in (select code from B where pcode = '111')
原因是子查询的返回结果是多个。select * from A where A.code IN (select code from B where pcode LIKE '111%';
select code from B where pcode='111'这个的查询结果只有一个。这个sql的功能是找到A中以括号内查询结果为前缀的所有结果
Quote=引用 4 楼 shiyoumaomao 的回复:] select code from B where pcode='111'这个的查询结果只有一个。 [/Quote] 那code的查询结果肯定是重复的 select * from A where A.code like (select distinct code from B where pcode='111')||'%'
code的结果也是唯一的,由pcode可以唯一的得到code,由code也可以获得唯一的pcode可是sql在DB2中就是没法运行啊把语句改成select * from A where A.code =(select distinct code from B where pcode='111')一点问题没有但是我想查询的是:找到A中以括号内查询结果为前缀的所有结果各位高手有没有什么好的解决办法?
select A.* from A ,B where instr(A.code,B.code)=1; A,B做笛卡尔乘积,然后在A表的code字段找B表的code字段,instr(A.code,B.code)=1,返回从左第一位找到的行(前缀)。
红字部分查询的code结果不止一个吧
from A
where A.code in (select code from B where pcode = '111')
select code from B where pcode='111'这个的查询结果只有一个。
[/Quote]
那code的查询结果肯定是重复的
select * from A where A.code like (select distinct code from B where pcode='111')||'%'
A,B做笛卡尔乘积,然后在A表的code字段找B表的code字段,instr(A.code,B.code)=1,返回从左第一位找到的行(前缀)。