《mysql必知必会》学习_第14章_20180806_欢
第14章:使用子查询。
子查询是镶嵌在其他查询里面,相当其他的select查询的条件来。
P91
select order_num from where prod_id='tnt2'; #检索条件为prod_id=tnt2的order_num#
select cust_id from orders where order_num in (20005,20007); #检索满足条件 order_num在范围(20005,20007)的cust_id#

select cust_id from orders where order_num in (select order_num from orderitems where prod_id ='tnt2' ); #检索 满足条件:///在表orderitems中满足prod_id=tnt2 的order_num,orderitems.order_num =orders.order_num /// 这部分的order_num在orders中对应的cust_id # ##实际上,就是上面的两个语句组合而成的##

P92
select cust_name,cust_contact from customers where cust_id in (10001,10004); #检索表customers满足客户Id在范围(10001,10004)内,对应的cust_name,cust_contact #

select cust_name,cust_contact from customers where cust_id in (select cust_id from orders where cust_num in (select cust_num from orderitems where prod_id ='tnt2') ); #镶嵌了两个子查询,从最里面的括号的条件开始运行#

P94
select count(*) as orders from orders where cust_id=10001; #检索cust_id=10001的个数#
select cust_name,cust_state,(select count(*) from orders where orders.cust_id =customers.cust_id) as orders from customers order by cust_name ; #关联两个表,检索满足条件orders.cust_id = customers.cust_id 的行数(count(*)),cust_name,cust_state #

注意:关联多个表的时候,要指明列的时候,表示哪个表哪个列的时候:表点列(如customers.cust_name) !!!
《mysql必知必会》学习_第14章_20180806_欢的更多相关文章
- 《mysql必知必会》学习_第15章_20180806_欢
第15章:联结表 P98 外键:外键为某个表的一列A,同时这一列包含另一个表的主键值B(B属于A,等于或者小于的关系) P99 select vend_name,prod_name,prod_pric ...
- 《mysql必知必会》学习_第18章_20180807_欢
第18章 全文本搜索 P121 #创建一个新表,对表的列进行定义,定义之后,MySQL自动维护该索引# create table productnotes ( note_id int NOT ...
- 《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必知必会》学习_第17章_20180807_欢
第17章:组合查询 P114 select vend_id ,prod_id,prod_price from products where prod_price <=5 ; select ven ...
- 《mysql必知必会》学习_第16章_20180807_欢
第16章:创建高级联结. P106 select concat(RTrim(vend_name),'(',RTrim(vend_country),')') as vend_title from ven ...
- 《mysql必知必会》学习_第13章_20180803_欢
第13章:分组过滤. P83 select count(*) as num_prods from products where vend_id=1003; #返回vend_id=1003的产品数目总值 ...
随机推荐
- [Oracle,2018-02-07] Oracle 报错:“ORA-02292:违反完整约束条件(XXX.FKXXX)
报错的原因很清楚,就是你要删除的记录是另外某条记录的外键,解决办法: 1.删除子记录,在删除本记录: 2.暂时禁用此外键(适合在测试后清除所有记录的情况,记得最后要恢复此外键) 解决办法有了,现在最主 ...
- Python的随机数模块
random模块中几个随机函数用法. 引入random模块: import random 1.random.random() 此函数用于生成一个0到1的随机浮点数:0 <= n < 1.0 ...
- Android 开发 AlarmManager 定时器
介绍 AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为 ...
- python基础数据篇
1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ? 1 names = ['Alex',"Tenglan",' ...
- leetcode139
class Solution { public: bool wordBreak(string s, vector<string> wordDict) { vector<, false ...
- selenium 3.0变化
Selenium3.0的变化 最大的变化应该是去掉了Selenium RC 了,这是必然的结果.Selenium RC 是Selenium1.0的产物,Selenium2.0以WebDriver为主, ...
- JavaScript:BOM&DOM
BOM(Browser Object Model): feature: 1.BOM has no relevant standards. 2.The fundamental object of BOM ...
- 用react编写一个可以编辑的表格
这只一个雏形,但是可以用了.难点是如何点击每行后面的编辑按钮,让当前行的格子都变成input. import {Component} from 'react' const Action = props ...
- R语言-简单模型画图
1.回归拟合 > plot(mtcars$mpg~mtcars$disp) > lmfit<-lm(mtcars$mpg~mtcars$disp) #线性回归模型 > abli ...
- django admin 模块添加 static
目前路径是在 /var/www/jastme/static 但是admin的静态文件不在这个位置,那么怎么办呢? 首先到项目下,就是有manage.py的这个目录 编辑项目下的setting.py 首 ...