select a.* from A a 
where exists ( select 1 from B b where a.id=b.id )

 public List exist(){
  List result;
  Array A=(select * from A)   for(int i=0; i<A.length; i++) {
  if(exists(A[i].id) { //执行select 1 from B b where b.id=a.id是否有记录返回
  result.add(A[i]);
  }
  }
  return result;
}

2.in

select * from A
where id in ( select id from B )

 public List in(){
  List result;
  Array A = (select * from A);
  Array B = (select id from B);   for(int i=0; i<A.length; i++) {
  for(int j=0; j<B.length; j++) {
  if(A[i].id == B[j].id) {
  result.add(A[i]);
  break;
  }
  }
  }
  return result;
}

A表10000条记录,B表1000000条记录,那么最多有可能遍历10000*1000000次,效率很差.

A表10000条记录,B表100条记录,那么最多有可能遍历10000*100次,遍历次数大大减少

结论:

子查询表大的用exists,子查询表小的用in

3. in与 =

select name from student where name in ('zhang','wang','li','zhao');

select name from student where name='zhang' or name='li' or name='wang' or name='zhao';

exist & in的更多相关文章

  1. Android之Dedug--Circular dependencies cannot exist in AnimatorSet

    今日,在学习AnimatorSet时,使用play.with.after.before时,代码书写如下: ObjectAnimator animator1 = ObjectAnimator.ofFlo ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. MySQL: Table 'mysql.plugin' doesn't exist的解决

    安装解压版MySQL以后,不能启动,日志里面出现了这个错误: MySQL: Table 'mysql.plugin' doesn't exist 这是因为mysql服务启动时候找不到内置数据库&quo ...

  4. 解决 release-stripped.ap_' specified for property 'resourceFile' does not exist.

    设置buildTypes里的release的shrinkResources为false即可,如果是 release-stripped.ap_' specified for property 'reso ...

  5. Mac 下locate命令使用问题WARNING: The locate database (/var/db/locate.database) does not exist.

    想在Mac下使用locate时,提醒数据库没创建: WARNING: The locate database (/var/db/locate.database) does not exist. To ...

  6. CS0103: The name ‘Scripts’ does not exist in the current context解决方法

    转至:http://blchen.com/cs0103-the-name-scripts-does-not-exist-in-the-current-context-solution/ 更新:这个bu ...

  7. 执行mysqld_safe报错:mysqld does not exist or is not executable

    执行mysqld_safe报错: [root@edu data]# /usr/local/mysql5.7/bin/mysqld_safe --user=mysql160427 12:41:28 my ...

  8. tomcat报错java.lang.IllegalArgumentException: Document base XXXXX does not exist or is not a readable directory

    启动tomcat的时候报如下错误: java.lang.IllegalArgumentException: Document base F:\java\tools\tomcat\me-webapps\ ...

  9. mysqldump:Couldn't execute 'show create table `tablename`': Table tablename' doesn't exist (1146)

    遇到了一个错误mysqldump: Couldn't execute 'show create table `CONCURRENCY_ERRORS`': Table INVOICE_OLD.CONCU ...

  10. mysql [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist (转载)

    mysql报错Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 2013-11-2 ...

随机推荐

  1. 转: Your build settings specify a provisioning profile with the UUID, no provisioning profile was found

    http://blog.csdn.net/rbyyyblog/article/details/12220875 在Archive项目时,出现了“Your build settings specify ...

  2. python3 读取csv的常用语法

    import csv #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() with open("info.csv" ...

  3. html 标签 链接

    <a href="http://www.baidu.com">百度</a> <a href="#here">here< ...

  4. 随机生成数,摘自算法竞赛入门经典P120-P123测试STL。

    //#include<bits/stdc++.h> #include<cstring> #include<iostream> #include<cstdio& ...

  5. All in All - poj 1936 (子串)

    字符串子序列查找问题,设置两个指针,一个指向子序列,另一个指向待查找的序列,查找个字符串一次即可判断.   #include <iostream> #include <string. ...

  6. Servlet 服务器 HTTP 响应

    状态行包括 HTTP 版本(在本例中为 HTTP/1.1).一个状态码(在本例中为 200)和一个对应于状态码的短消息(在本例中为 OK). 下表总结了从 Web 服务器端返回到浏览器的最有用的 HT ...

  7. HTML <td> 标签的 width 属性

    <table border="1" width="100%"> <tr> <th>Month</th> < ...

  8. java算法学习

    最大公约数 欧几里得算法 描述:计算两个非负整数p和q的最大公约数: 若q是0,则最大公约数为p. 否则,将p除以q得到余数r,p和q的最大公约数即为q和r的最大公约数. 根据算法的自然描述,我们可以 ...

  9. HDU 3080 The plan of city rebuild(除点最小生成树)

    题意  一个城市原来有l个村庄 e1条道路  又添加了n个村庄 e2条道路  后来后销毁了m个村庄  与m相连的道路也销毁了  求使全部未销毁村庄相互连通最小花费  不能连通输出what a pity ...

  10. -webkit-transition: all .2s ease-in-out;

    W3C标准中对CSS3的transition这是样描述的:CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发,并 ...