SQL有SQL92与SQL99两个重要的标准版本,两个版本中的连接查询方式不太相同。在平常写的过程中容易混淆。
- 笛卡尔积:
from a,b
# 等同于
from a cross join b
- 等值连接:
from a,b where a.f1=b.f1
# 等同于
from a join b on a.f1=b.f1
- 自然连接:
from a,b where a.f1=b.f1 and a.f2=b.f2
# 等同于:
from a natural join b
注意: natural join 会把进行连接的两个表中相同的字段 进行等值连接
- USING:
from a,b where a.f1=b.f1 and a.f2=b.f2
# 等同于:
from a join b using (f1,f2)
注意:join与natural不同在于,不会把所有字段都进行等值连接,而是根据using后面指定定的字段。且必须是from a join b using()格式,不能是 from a,b using()