left join on 查询
类别:数据库 浏览量:2830
时间:2014-5-7 left join on 查询
left join on 查询LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。也就是说:左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.bID). B表记录不足的地方均为NULL.
操作实例
1、表a结构和数据
table a(id, type):
id type
----------------------------------
1 1
2 1
3 2
2、表b结构和数据
table b(id, class):
id class
---------------------------------
1 1
2 2
3、SQL语句调用
--sql语句1:
select a.*, b.* from a left join b on a.id = b.id and a.type = 1;
--sql语句2:
select a.*, b.* from a left join b on a.id = b.id where a.type = 1;
--sql语句3:
select a.*, b.* from a left join b on a.id = b.id and b.class = 1;
4、sql语句1的执行结果为
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
3 2
5、sql语句2的执行结果为
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
6、sql语句3的执行结果为
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1
3 2
7、left join实例的备注
由sql语句1可见,left join 中左表的全部记录将全部被查询显示,on 后面的条件对它不起作用,除非再后面再加上where来进行筛选,这就是sql语句2了;由sql语句3可见,on后面的条件中,右表的限制条件将会起作用。
热门推荐
- ui界面的测试用例(AmazeUI 模态窗口的实现代码)
- sqlserver分页查询sql语句(sql server实现分页的方法实例分析)
- php依赖注入对象注册(php依赖注入知识点详解)
- MySQL中NULL和空值的区别
- 云服务器弹性体现在哪些方面(云服务器的性能表现在哪些方面?)
- python实现简单加密(Python 隐藏输入密码时屏幕回显的实例)
- sqlserver数据类型和长度(SqlServer 数据库 三大 范式)
- sql分区原理(分区表场景下的 SQL 优化)
- laravel查询构建器原理和使用(阿里对象存储OSS在laravel框架中的使用方法)
- sql server 锁超时(详解SQL Server中的事务与锁问题)