MY SQL高级连接

自然连接的方法:

--查询出产品id为FB的,顾客信息,和订单数量,订单日期,一件商品的价格

 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 o.order_num=oi.order_num
 and prod_id='FB'

为了检索ode所有客户,包括哪些没有订单的客户。

已做为主。要的是客户,客户左链接

 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 o.order_num=oi.order_num
 and prod_id='FB'

16.3 使用带聚集函数的连接

--检索客户对应的每个客户对应的订单数目

select customers.cust_name,customers.cust_id,
       count(orders.order_num)

from customers inner join orders
on customers.cust_id=orders.cust_id
group by customers.cust_id

不分组的话出现:

显示的列表名就只有1条,统计是5个。和事实不符。

??为什么出现内联和外联的区别是什么呢?

使用做外链接的方式李艾包含所有客户,甚至包含没有订单的客户。

select customers.cust_name,customers.cust_id,
       count(orders.order_num)

from customers left join orders
on customers.cust_id=orders.cust_id
group by customers.cust_id

猜你喜欢

转载自blog.csdn.net/xiamaocheng/article/details/82226587
my