如下两个语句,有什么区别,会带来什么问题?
selet * from t1 where t1.id in (select id from t2 where t2.name like '%test%')selet * from t1 where exists (select id from t2 where t2.name like '%test%')

解决方案 »

  1.   

    MYSQL会使用不同的机制来执行。速度上会有差别。
      

  2.   

    selet * from t1 where t1.id in (select id from t2 where t2.name like '%test%')selet * from t1 where exists (select id from t2 where t2.name like '%test%')==>第一个语句是要将2表关联起来 条件是 2表的id字段 只能返回t1的部分记录或者全部记录 
    第二个语句是只要(select id from t2 where t2.name like '%test%')为真 就是存在记录 那么就返回整个t1表的记录 2个语句是不一样的 
      

  3.   

    两个语句结果不一致
    2只要存在t2.name like '%test%',则返回全部记录
    1返回ID 在 (select id from t2 where t2.name like '%test%')中的记录