第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_欢的更多相关文章

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

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

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

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

  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必知必会》学习_第17章_20180807_欢

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

  8. 《mysql必知必会》学习_第16章_20180807_欢

    第16章:创建高级联结. P106 select concat(RTrim(vend_name),'(',RTrim(vend_country),')') as vend_title from ven ...

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

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

随机推荐

  1. 转: spring静态注入

    与其说是静态注入(IOC),不如讲是对JavaBean 的静态成员变量进行赋值. 一般我们在使用依赖注入的时候,如果当前对象(javaBean )创建(实例化)一次,那么非静态的成员变量也会实例化一次 ...

  2. leetCode21: 合并两个有序列表

    将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2- ...

  3. Spring -- <tx:annotation-driven>注解基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)的区别。

    借鉴:http://jinnianshilongnian.iteye.com/blog/1508018 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional ...

  4. ExecutorService——shutdown方法和awaitTermination方法

    ExecutorService的关闭shutdown和awaitTermination为接口ExecutorService定义的两个方法,一般情况配合使用来关闭线程池. 方法简介shutdown方法: ...

  5. mysql 表结构

    1.登录数据库>mysql -u root -p 数据库名称 2.查询所有数据表>show tables; 3.查询表的字段信息>desc 表名称; 4.1添加表字段 alter t ...

  6. Ubuntu下解决MySQL自启动,chkconfig list 全部off 情况

    chkconfig命令是用于RedHat/Fedora发行版的,而对于像Ubuntu之类的Debian发行版,应该使用这个命令: sudo update-rc.d mysql defaults 验证一 ...

  7. FP数据库配置文件

  8. 如何将 jar 包导入Maven 本地仓库

    案例:oracle jar包由于在maven 远程仓库中找不到,需要先将oracle jar 文件下载到本地,然后导入maven本地仓库,就可以通过 pom 进行依赖 例如:下载后的 jar 地址 D ...

  9. Django 学生信息 添加 功能 遇到的问题.

    1  添加 班级信息时的问题 (grade为外键) 原因是 grade 必需接收 一个 实例, 而我交是一个 str字符串, if request.method == 'POST': data = { ...

  10. Django 表关系

    1.自定义主键字段的创建 AutoFiled(pirmary_key=True) # 一般不会自定义2.order_by asc desc 1. 表关系的创建- OneToOne student = ...