《mysql必知必会》学习_第16章_20180807_欢
第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_欢的更多相关文章
- 《mysql必知必会》学习_第18章_20180807_欢
第18章 全文本搜索 P121 #创建一个新表,对表的列进行定义,定义之后,MySQL自动维护该索引# create table productnotes ( note_id int NOT ...
- 《mysql必知必会》学习_第17章_20180807_欢
第17章:组合查询 P114 select vend_id ,prod_id,prod_price from products where prod_price <=5 ; select ven ...
- 《mysql必知必会》学习_第五章_20180730_欢
使用的工具是wamp的Mysql. P29 select prod_name from products; #在表products中选列prod_name,顺寻不是纯粹的随机,但是没有说明排列顺序, ...
- 《mysql必知必会》学习_第22章_20180809_欢
第22章:使用视图,视图是虚拟的表,以表形式呈现的是你查询的结果.并不是说在数据库里面真的存在这个表,但是是真的存在这些数据. select cust_name,cust_contact from c ...
- 《mysql必知必会》学习_第20章_20180809_欢
第20章:更新和删除数据 P140 update customers set_emails='elmer@fudd.com' where cust_id=10005; 更新多个列,用逗号隔开.注意被指 ...
- 《mysql必知必会》学习_第19章_20180809_欢
第19章 插入数据 P132 insert into customers VALUES(NULL,'Pep E.Lapew','100 Main Street',,Los Angeles','CA', ...
- 《mysql必知必会》学习_第15章_20180806_欢
第15章:联结表 P98 外键:外键为某个表的一列A,同时这一列包含另一个表的主键值B(B属于A,等于或者小于的关系) P99 select vend_name,prod_name,prod_pric ...
- 《mysql必知必会》学习_第14章_20180806_欢
第14章:使用子查询. 子查询是镶嵌在其他查询里面,相当其他的select查询的条件来. P91 select order_num from where prod_id='tnt2'; #检索条件 ...
- 《mysql必知必会》学习_第13章_20180803_欢
第13章:分组过滤. P83 select count(*) as num_prods from products where vend_id=1003; #返回vend_id=1003的产品数目总值 ...
随机推荐
- Java 四种引用介绍及使用场景
强引用-FinalReference 介绍: 强引用是平常中使用最多的引用,强引用在程序内存不足(OOM)的时候也不会被回收,使用方式: String str = new String("s ...
- Android 开发 深入理解Handler、Looper、Messagequeue 转载
转载请注明出处:http://blog.csdn.net/vnanyesheshou/article/details/73484527 本文已授权微信公众号 fanfan程序媛 独家发布 扫一扫文章底 ...
- asp.net 微信JsSDK
有时间再整理吧 using System; using System.Collections.Generic; using System.Linq; using System.Web; using S ...
- Java面试——线程池
1.类比介绍 假如有一个工厂,工厂里面有10个工人,每个工人同时只能做一件任务. 因此只要当10个工人中有工人是空闲的,来了任务就分配给空闲的工人做: 当10个工人都有任务在做时,如果还来了任务,就把 ...
- alias命令详解:给命令设置别名
给命令设置别名,你可以把它当作命令的"小名",但是这样做有什么意义呢? 比如笔者刚接触 Linux 时,使用的编辑器是 Vi,但是现在 Vim 的功能明显比 Vi 的功能更加强大, ...
- C#面向对象架构总结
优秀的程序从命名开始,命名规则1.类名方法名需要见名知意.2.类的字段统统首字母小写并且封装仅供本类使用,并提供属性供外界使用,属性名开头大写,如果名字与类名冲突,那么在前面加下划线. 面向对象的7大 ...
- C#//字节数组转16进制字符串
//字节数组转16进制字符串 private static string byteToHexStr(byte[] bytes,int length) { string returnStr = &quo ...
- linux安装redis-4.0.2
一.下载redis cd /usr/local/src wget http://download.redis.io/releases/redis-4.0.2.tar.gz 二.解压 tar -zxvf ...
- 大数据spark学习第一周Scala语言基础
Scala简单介绍 Scala(Scala Language的简称)语言是一种能够执行于JVM和.Net平台之上的通用编程语言.既可用于大规模应用程序开发,也可用于脚本编程,它由由Martin Ode ...
- NLTK 统计词频
import nltk Freq_dist_nltk = nltk.FreqDist(list) for k,y in Freq_dist_nltk: print str(k),str(y)