表table1
       id1 id2
行一   01  02
行二   01  03
行三   。
 
表table2
id
01
03
04
如何高效的判断表table1某一行数据的id1、id2同时出现在table2之中?

解决方案 »

  1.   


      SELECT COUNT(id1) FROM table1 INNER JOIN table2 ON table1.id1 = table2.id OR table1.id2 = table2.id
    HAVING COUNT(id1) > 1
      

  2.   

    to lengyuehui:
    这个方法不行,比如tabel2中只有01,也会被提示。正常情况下是在表2中同时出现01、02或者01、03才会提示的。
      

  3.   

    可以用in或者existsselect * from table1 where id1 in (select id from table2) and id2 in (select id from table2)select * from table1 a where exists (select id from table2 where id = a.id1) and exists (select id from table2 where id = a.id2)