第16章:创建高级联结。

P106

select concat(RTrim(vend_name),'(',RTrim(vend_country),')') as vend_title from vendors order by vend_name;   #concat()函数合并两列,并重新命名合并的列为vend_name。#

select cust_name,cust_contact from customers as c,orders as o,orderitems as oi where c.cust_id =0.cust_id and oi.order_num = o.order_num and prod_id ='tnt2'  # from之后重新命名了表customers为c ,命名表orders为0,命名orderitems为oi ,在where语句后面使用新命名的表名,这就减少了表名太长和需要多次键入表名的麻烦#

使用不同类型的联结,联结分为:自联结(A和A自身联结),//自然联结,外部联结 (不理解)/

P107 自联结

select prod_id,prod_name from products where vend_id =(select vend_id from products where prod_id ='dtntr') ; #满足 条件表products中prod_id=dtnr所对应的vend_id ,这些vend_id在products中所对应的prod_id,prod_name #  ##只是在表products里面兜,其实相当先检索vend_id=1003,vend_id=1003时,prod_id,prod_name有下面图一那些。其实就是图二的转换顺序红色--绿色--黄色(红色箭头方向不正确)##

P108

select p1.prod_id,p1.prod_name from products as p1 ,products as p2 where p1.vend_id =p2.vend_id and p2.vend_id ='dtntr' ; #where之后的条件其实是vend_id=1003,和上面的语句表达的意思一样#

P109 自然联结 #多个表联结,表的每个列只返回一次,不像自联结在同一个表的列不断的兜来兜去(我理解的)#

select c.* ,o.order_num,o.order_date ,oi.prod_id ,oi.quantity ,oi.item_price from customers as c,orders as o,orderitems as oi where c.cust_id = o.cust_id and oi.order_num =o.order_num and prod_id ='fb';  #主要是仔细看好重新命名的名称,这个语句逻辑关系很清晰#

P109 外部联结

P110 内部联结:select customers.cust_id ,orders.order_num from customers inner join orders on customers.cust_id =orders.cust_id ;

P 110外部联结:select customers.cust_id ,orders.order_num from customers left outer join orders on customers.cust_id =orders.cust_id ; #outer join指定了联结类型为外部联结,left表示 outer join 左边的表,由左边的表去对应右边的表,比如上面的语句,customer.cust_id存在等于10002,但是orders.cust_id不存在10002,所以显示order_num=null ,但是如果是右联结,就不会存在cust_Id=10002,因为orders.cust_id不存在10002#

P111 select customers.cust_name ,customers.order_id,count(orders.order_num) as num_ord from customers innter join orders on customers.cust_id =orders.cust_id group by customers.cust_id ;  # 条件:customers.cust_id =orders.cust_id group by customers.cust_id  ,其他的对应上就是了#

select customers.cust_name ,customers.order_id,count(orders.order_num) as num_ord from customers left outer join orders on customers.cust_id =orders.cust_id group by customers.cust_id ;  # 条件:customers left outer join orders,左边的表对应右边的表,左边的表里面的存在cust_id=10002,所以得到的cust_id里面有cust_id=10002;#

《mysql必知必会》学习_第16章_20180807_欢的更多相关文章

  1. 《mysql必知必会》学习_第18章_20180807_欢

    第18章 全文本搜索 P121  #创建一个新表,对表的列进行定义,定义之后,MySQL自动维护该索引# create table productnotes ( note_id  int   NOT ...

  2. 《mysql必知必会》学习_第17章_20180807_欢

    第17章:组合查询 P114 select vend_id ,prod_id,prod_price from products where prod_price <=5 ; select ven ...

  3. 《mysql必知必会》学习_第五章_20180730_欢

    使用的工具是wamp的Mysql. P29 select prod_name from products;  #在表products中选列prod_name,顺寻不是纯粹的随机,但是没有说明排列顺序, ...

  4. 《mysql必知必会》学习_第22章_20180809_欢

    第22章:使用视图,视图是虚拟的表,以表形式呈现的是你查询的结果.并不是说在数据库里面真的存在这个表,但是是真的存在这些数据. select cust_name,cust_contact from c ...

  5. 《mysql必知必会》学习_第20章_20180809_欢

    第20章:更新和删除数据 P140 update customers set_emails='elmer@fudd.com' where cust_id=10005; 更新多个列,用逗号隔开.注意被指 ...

  6. 《mysql必知必会》学习_第19章_20180809_欢

    第19章 插入数据 P132 insert into customers VALUES(NULL,'Pep E.Lapew','100 Main Street',,Los Angeles','CA', ...

  7. 《mysql必知必会》学习_第15章_20180806_欢

    第15章:联结表 P98 外键:外键为某个表的一列A,同时这一列包含另一个表的主键值B(B属于A,等于或者小于的关系) P99 select vend_name,prod_name,prod_pric ...

  8. 《mysql必知必会》学习_第14章_20180806_欢

    第14章:使用子查询. 子查询是镶嵌在其他查询里面,相当其他的select查询的条件来. P91 select order_num from where prod_id='tnt2';   #检索条件 ...

  9. 《mysql必知必会》学习_第13章_20180803_欢

    第13章:分组过滤. P83 select count(*) as num_prods from products where vend_id=1003; #返回vend_id=1003的产品数目总值 ...

随机推荐

  1. 浅谈Cookie与Session技术

      一.什么是状态管理 将客户端与服务器之间多次交互当做一个整体来看,并且将多次交互所涉及的数据(状态)保存下来. 会话:当用户打开浏览器,访问多个WEB资源,然后关闭浏览器的过程,称之为一个会话,选 ...

  2. mybatis拦截器获取sql

    mybatis获取sql代码 package com.icourt.alpha.log.interceptor; import org.apache.ibatis.executor.Executor; ...

  3. spring-AOP之通知和顾问

    通知和顾问都是切面的实现形式,其中通知可以完成对目标对象方法简单的织入功能. 而顾问包装了通知,可以让我们对通知实现更加精细化的管理,让我们可以指定具体的切入点. 通知分为前置通知,环绕通知及后置通知 ...

  4. Java学习笔记(十四):java常用的包

  5. 深入JVM之类的加载器

    类加载器有两种: —java虚拟机的自带加载器 根类加载器(Bootstrap) 扩展类加载器(Extension) 系统类加载器(AppClassLoder) —自定义的类加载器 java.lang ...

  6. 语义分割之Dual Attention Network for Scene Segmentation

    Dual Attention Network for Scene Segmentation 在本文中,我们通过 基于自我约束机制捕获丰富的上下文依赖关系来解决场景分割任务.       与之前通过多尺 ...

  7. 【python中二叉树的实现】python中二叉树的创建、三种方式递归遍历和非递归遍历

    代码如下: # coding=utf-8 class myNode(object): def __init__(self, data=-1, lchild=None, rchild=None): se ...

  8. DIV内容超出长度显示省略号,鼠标移上自动显示全部内容(EasyUI DataGrid)

    如果想把DIV中超出的文本显示成省略号,而不是换行全部显示,有2个办法. 注:本文主要是以EasyUI的DataGrid为案例的,如果是其他场景只要底层是用DIV显示文本的应该都能使用. 首先可以给此 ...

  9. stark组件开发之列表页面应用示例

    已经解决的,自定义的扩展函数,功能.但是 不可能返回. 一个 固定的页面把!  应该是,点击那条 记录之后的编辑, 就会跳转到相应的,编辑页面.所以 这个标签的  <a href="/ ...

  10. swap的实现(没有中间变量)

    两数交换最常用的方法 void swap(int &a,int &b) { int temp=a; a=b; b=temp; } 如果没有中间变量temp可以使用呢,还有其他的三种方法 ...