第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. 阿里云RDS读写分离数据查询延迟解决

    mysql使用RDS做数据主从读写分离.在使用的过程中发现部分业务对其他服务以来严重.但是由于系统不是采用微服务的架构,造成部分数据插入数据库后,后续操作读取数据库没有查询到前面插入的数据.查看阿里云 ...

  2. PHP/TP5 接口设计中异常处理

    PHP提供 Exception 类来处理异常 new Exception('错误信息(默认为空)','错误代码(默认0)','异常链中前一个异常') 然后可以通过 e -> getMessage ...

  3. 学习node.js 第2篇 介绍node.js 安装

    Node.js - 环境安装配置 如果愿意安装设置Node.js环境,需要计算机上提供以下两个软件: 一.文本编辑器 二.Node.js二进制安装包 文本编辑器 这将用来编写程序代码. 一些编辑器包括 ...

  4. English-英语日常交流语句

  5. nexus的安装和简介(2)

    上传jar包到私服 1. 配置settings.xml 需要在客户端即部署dao工程的电脑上配置 maven环境,并修改 settings.xml 文件,配置连接私服的用户和密码 . 此用户名和密码用 ...

  6. Java学习笔记(二十一):类型转换和instanceof关键字

    基本数据类型转换: 自动类型转换:把大类型的数据赋值给大类型的变量(此时的大小指的是容量的范围) byte b = 12; //byte是一个字节 int i = b; //int是四个字节 强制类型 ...

  7. Spring使用fastjson处理json数据

    1.搭建SpringMVC+spring环境 2.配置web.xml以及springmvc-config.xml,web.xml同Spring使用jackson处理json数据一样,Springmvc ...

  8. Numpy三维数组的转置与交换轴

    二维数组的转置应该都知道,就是行列交换 而在numpy中也可以对三维数组进行转置,np.T 默认进行的操作是将0轴与2轴交换 本文主要对三位数组轴交换的理解上发表本人的看法. a = np.array ...

  9. angular分页插件tm.pagination二次触发问题解决歪方案

    今天在学习angularjs的分页插件时遇到了一个前端的问题,谷歌浏览器开发者模式调试的时候发现每次点击分页刷新按钮会触发两次后台请求,ajax向后台发送了两次请求,这对于强迫症患者来说是一个比较恶心 ...

  10. Python设计模式 - UML - 类图(Class Diagram)

    简介 类图是面向对象分析和设计的核心,用来描述系统各个模块中类与类之间.接口与接口之间.类与接口之间的关系,以及每个类的属性.操作等特性,一般在详细设计过程中实施. 类图本身就是现实世界的抽象,是对系 ...