SQL嵌套查询
来源:互联网 发布:dreamhost java 编辑:程序博客网 时间:2024/06/05 06:56
SQL嵌套查询
利用自查询进行行过滤(in)
订单表orders
顾客表 customers
订单商品表orderitems
#列出订购物品RGAN01的所有顾客select cust_name,cust_contact from customers where cust_id in ( select cust_id from orders where order_num in ( select order_num from orderitems where prod_id='RGAN01'));
-作为自查询的SELECT语句只能查询单个列
作为计算字段使用子查询
显示customers表中每个顾客的订单总数
select cust_name,cust_contact,(select count(*) from orders where orders.cust_id = customers.cust_id)as orders from customers;
执行结果
如何理解orders表中子查询where orders.cust_id= customers.cust_id?
- orders表中的cust_id 字段为
customers表中的cust_id字段为
自查询中的select列表为count(*)
其含义为对customers表(上一级查询中的表)中cust_id列的所有项(1000000001、1000000002、1000000003、1000000004、1000000005)按照orders表(子查询的表)中cust_id列出现的次数进行计数
5.该子查询对检索出来的每个顾客执行一次, 得到结果
cust_id
1000000001 2
1000000002 0
1000000003 1
1000000004 1
1000000005 1
阅读全文
0 0
- sql嵌套子查询
- SQL 嵌套多层查询
- sql嵌套查询
- SQL嵌套子查询
- SQL嵌套查询
- SQL嵌套子查询
- SQL嵌套查询
- SQL 嵌套查询问题
- sql 嵌套查询
- SQL 实现嵌套查询
- sql嵌套查询学习
- SQL嵌套查询
- sql语句嵌套查询
- SQL语句 - 嵌套查询
- SQL语句 - 嵌套查询
- sql复杂嵌套查询
- SQL语句 - 嵌套查询
- SQL语句 - 嵌套查询
- DEBUG_NEW未定义错误
- C++语言新特性
- 4K显示器设置小记
- 前端jquery treeTable动态绑定数据
- 未将对象引用到对象的实例
- SQL嵌套查询
- 进程与线程的一个简单解释
- Spring+SpringMVC+Hibernate+Maven的整合
- Mac系统10.11(10.12)之后/usr/bin 不能拷贝进去class-dump该怎么办
- OSGi bundle activator不工作解决
- [Python] windows下360急速浏览器获取URL的Headers
- 数据结构-双向链表
- 8月10日总结
- Qt编译遇到/usr/bin/ld: cannot find -lGL问题的解决方案