select * from a where EXP_CODE='R8_002_XZ' and code not in (select code from b)=======================================================select * from a left join b on a.code=b.code
where a.EXP_CODE='R8_002_XZ' and b.code is null再看你的code上有无索引,如无,建索引, exp_code 上也建
where a.EXP_CODE='R8_002_XZ' and b.code is null再看你的code上有无索引,如无,建索引, exp_code 上也建
用你的代码效率的确太高了,但在执行过程中又出的个问题
如果a表中有多条记录的CODE 相同,但EXP_CODE不同,执行后只会显示其中一条记录如:
a表中有:
CODE EXP_CODE
123456 R8_002_XZ
123456 R8_002b表中有:
CODE EXP_CODE
123456 R8_002_XZ如果查询:
select * from a left join b on a.code=b.code where a.EXP_CODE='R8_002' and b.code is null
将无法得到123456 R8_002 的记录
$sql="select * from a,b where a.CODE!=b.CODE and a.EXP_CODE!=b.EXP_CODE and a.CODE='R8_002_XZ'";找出的记录会是一个无限循环
上面的语法可能有点问题,但是逻辑关系应该是对的.
要求:version>4.1,支持子查询.
select * from a left join b on a.code=b.code and a.EXP_CODE=b.EXP_CODE
where a.EXP_CODE='R8_002' and b.code is null
select * from a where exp_code='r8_002_xz' and code not in(select code from b where exp_code!='r8_002_xz');
或者
select * from a where exp_code='r8_002_xz' and not exists(select code from b where exp_code!='r8_002_xz' and a.code=b.code);