第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. oracle入坑日记<五>数据表

    1   数据表 1.1.数据表是存放数据字段信息的地方:在Oracle,数据表拥有者单位是用户,同时数据表属于表空间.如: 登录my_user用户在orcl表空间下创建的表就是 my_user用户在o ...

  2. JVM 符号引用与直接引用

       Java类从加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括,加载 ,验证 , 准备 , 解析 , 初始化 ,卸载 ,总共七个阶段.其中验证 ,准备 , 解析 统称为连接.     ...

  3. 作着玩:登录页(纯css,不支持ie9以下)

    支持chrome FireFox 和 IE10+,(IE9也能显示,IE9以下不支持) <style type="text/css"> body{position:re ...

  4. .NET项目中使用PostSharp

    PostSharp是一种Aspect Oriented Programming 面向切面(或面向方面)的组件框架,适用在.NET开发中,本篇主要介绍Postsharp在.NET开发中的相关知识,以及一 ...

  5. leetcode494

    public class Solution { public int FindTargetSumWays(int[] nums, int S) { Queue<int> Q = new Q ...

  6. jqueryValidate

    参数详情可参见: http://www.runoob.com/jquery/jquery-plugin-validate.html 基本使用: /** 数据保存前校验 **/ $("#use ...

  7. Spring.Net配置

    <?xml version="1.0" encoding="utf-8"?> <configuration>     <!--一定 ...

  8. 使用idea生成maven项目的jar包(转)

    第一步  第二步  第三步 转自:https://blog.csdn.net/waterimelon/article/details/69243651

  9. Object.assign()怎么用?

    用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target). 1.以对象为参数的合并1)Object.assign方法的第一个参数是目标对象,后面的参数都是源对象 con ...

  10. 新建jsp项目

    选择 下面类型