left join on and
时间: 2023-11-27 16:02:39 浏览: 25
left join on和where子句的作用是筛选出符合条件的数据行,避免匹配到NULL值行。left join on是对左连接的右表进行条件过滤,但仍然会返回左表的所有内容,右表不满足on条件的行会被置为null。而where子句则是对查询结果进行条件过滤,只返回符合条件的数据行。
举个例子,假设有两张表ta和tc,我们想通过ta中的ida列和tc中的idc列进行左连接,同时筛选出tc中state为'001'的数据行。可以使用left join on和where子句的组合来实现这个目的。具体的SQL语句如下所示:
```sql
select * from ta left join tc on ta.ida=tc.idc and tc.state='001'
```
在这个例子中,left join on的部分是`on ta.ida=tc.idc and tc.state='001'`,表示在左连接的过程中,通过ida和idc进行匹配,并且只返回tc中state为'001'的数据行。而如果使用where子句来实现同样的功能,可以写成:
```sql
select * from ta left join tc on ta.ida=tc.idc where tc.state='001'
```
这个SQL语句的含义是先进行左连接,然后在连接的结果中筛选出tc中state为'001'的数据行。
总结起来,left join on和where子句都可以用来对左连接的右表进行条件过滤,只返回符合条件的数据行。但是left join on会返回左表的所有内容,右表不满足on条件的行会被置为null,而where子句则是在连接后对结果进行条件过滤。<span class="em">1</span><span class="em">2</span><span class="em">3</span>