当前位置: 首页 > news >正文

网站风格特点用自己电脑做网站服务器-phpstudy+花生壳

网站风格特点,用自己电脑做网站服务器-phpstudy+花生壳,如何做个小程序自己卖货,seo如何优化排名目录 一、知识点 二、测试验证 三、引申 一、知识点 left join中关于where和on条件的知识点: 多表left join 是会生成一张临时表。on后面: 一般是对left join 的右表进行条件过滤,会返回左表中的所有行,而右表中没有匹配上的数…

目录

一、知识点

二、测试验证

三、引申


一、知识点

         left join中关于where和on条件的知识点:

  • 多表left join 是会生成一张临时表。
  • on后面: 一般是对left join 的右表进行条件过滤,会返回左表中的所有行,而右表中没有匹配上的数据直接补为null。
  • on后面:如果存在对左表的筛选条件,无论条件真假,仍返回左表中的所有行,只是会影响右表的匹配值。即:on中针对左表的限制条件只会影响右表的匹配内容,并不影响返回的左表行数。
  • where后面:是对两表连接后的数据进行筛选,一般是针对左表的筛选条件。

   例如下面的sql:关联表(右表)的筛选条件如果放置在on后面,则下面sql的执行顺序是:B先按照条件进行过滤,再与A表关联; 

#主表:A; 关联表:B  关联条件:A.id = B.id  筛选条件:B.id >1
A left join B on A.id = B.id and B.id >1;

    筛选条件如果放置在where后面,则下面sql的执行顺序是:A和B先关联,基于生成的临时表再进行where条件过滤。

#主表:A; 关联表:B  关联条件:A.id = B.id  过滤条件
A left join B on A.id = B.id  where  A.id <> 1

二、测试验证

         创建t1 ,t2 两张表

create table if not exists test1( id int comment '用户id',name string comment '用户姓名');insert overwrite table test1
values (1,'a1'),(2,'a2'),(3,'a3'),(4,'a4');create table if not exists test2( id int comment '用户id',province string comment '用户所在省份');insert overwrite table test1
values (1,'hainan'),(2,'beijing'),(5,'sichuan'),(6,'chongqing');
测试1:返回左表所有行,右表符合on条件可匹配上,不满足条件的直接补null
selecttest1.id,test1.name,test2.id,test2.province
from test1
left join test2on test1.id = test2.id;

测试2:on后面增加对右表的限制条件;结论:左表记录全部返回,右表筛选条件生效
selecttest1.id,test1.name,test2.id,test2.province
from test1
left join test2on test1.id = test2.id and test2.province = 'beijing';

测试3:where后面增加对右表的限制条件:where test2.province = 'beijing';
   结论:where后面是对最后的临时表进行记录筛选,行数可能会减少
selecttest1.id,test1.name,test2.id,test2.province
from test1
left join test2on test1.id = test2.id  where test2.province = 'beijing';

测试4:on中增加对左表的限制条件,不影响返回的行数,只影响右表的匹配内容。
selecttest1.id,test1.name,test2.id,test2.province
from test1
left join test2on test1.id = test2.id and test1.name = 'a1';
测试5:where后面增加对左表的限制条件:where test1.name ='a4' 结论:where条件是在最后临时表的基础上进行筛选,返回满足条件的行
selecttest1.id,test1.name,test2.id,test2.province
from test1
left join test2on test1.id = test2.idwhere test1.name = 'a4';

三、引申

        由上述的where和on之间的区别,引申出来的面试题: t1表和t2表的字段如图,计算以下两个sql的值。

--1.输出结果
select  t1.id,t2.id
from t1
left join t2 on t1.id = t2.id and t2.id <>2;--2.输出结果
select  t1.id,t2.id
from t1
left join t2 on t1.id = t2.id 
where t2.id <>2;

解析:

(1)on后面:跟着的是对右表的限制条件  t2.id <>2;

          结论:左表记录全部返回,右表筛选条件生效

  
with t1 as (select 1 as id union allselect 2 as id union allselect 3 as id
),t2 as (select 1 as id union allselect 2 as id union allselect 2 as id)select  t1.id,t2.id
from t1
left join t2 on t1.id = t2.id and t2.id <>2;

输出结果为:

(2)where后面:跟着的是对右表的限制条件  where t2.id <>2;

          结论:最后临时表的基础上进行筛选,最终返回符合where过滤条件的行;

          ps: sql中比较的结果一般有:true, false, null; 而where条件只会过滤出true的结果

with t1 as (select 1 as id union allselect 2 as id union allselect 3 as id
),t2 as (select 1 as id union allselect 2 as id union allselect 2 as id)select  t1.id,t2.id
from t1
left join t2 on t1.id = t2.id
where t2.id <>2;

输出结果为:

补充:

select (1>2)  --> FALSE
select (1<2)  --> true
select (1<null) --> null--所以,sql比较的结果会有三种:false, true, null
http://www.yayakq.cn/news/355478/

相关文章:

  • 天津网站建设的公司响应式网站建设准备
  • 网站内容建设包括什么wordpress 评分插件
  • 西安医疗网站制作个人做 网站2019
  • 网站开发的论文引言软件著作权申请费用
  • 网站首眉怎么做万界随机购物系统
  • 高明网站设计报价网站建设的经验之谈
  • 网站关于我们示例如何检查网站死链
  • 化妆品首页设计免费网站建设优化
  • 凡科建站网页版做魔杖网站
  • 秒赞网站建设cms快速建站
  • 上海怎样建设网站有源码怎么在本地网站搭建
  • 上海专业做网站建设公司常熟建设局网站
  • 网站编程技术培训网站建设公司排名
  • 四川鼎能建设集团网站珠海品牌网站建设
  • 仙游h5做网站专业做互联网招聘的网站有哪些内容
  • 建筑公司网站首页图片网站死链查询
  • 网站建设投标文件儿童玩具网站模板
  • 瓯北网站制作洱源名师工作室网站建设
  • 义乌网站开发建设企业网站e路护航官网下载
  • 商丘市住房和城乡建设厅网站站群系统哪个好用
  • 网站建设项目介绍桐梓网站建设
  • 如何通过建设一个网站赚钱动漫制作专业主要学什么
  • 如何建立一个网站及app山西做网站价格
  • 网页设计一个多少工资中山网站搜索优化
  • 最便宜手机网站建设wordpress 默认页面自适应
  • 专做女装拿货的网站外包app开发定制
  • 怎么建设一个自己的网站首页网站开发自学还是培训
  • 浙江省网站集约化建设江苏seo平台
  • 佛山手机网站建设二级域名免费解析
  • 网站建设后应该干什么北京做家教的的网站