子查询一般是将查询出来的结果作为其他查询的结果使用。使用子查询可以一次性的完成很多逻辑上需要多个步骤才能完成的SQL操作。但多数情况下,使用join替代子查询效率更高。
## 子查询方式
语句如下:
```
MySQL [test]> SELECT cust_name FROM customerinfo
WHERE CustomerID NOT in (SELECT CustomerID FROM salesinfo);
```
## 推荐join方式
join方式语句如下:
```
MySQL [test]> create table test1(a int not null primary key,b int,c char(20));
Query OK, 0 rows affected (2.64 sec)
MySQL [test]> create table test2(a int not null primary key,b int,c char(20));
Query OK, 0 rows affected (2.28 sec)
MySQL [test]> insert into test1(a,b,c) values(1,2,'test1_record1')