无联结表的select

oderes表内容:

customers表内容:

 

1>

select customers.cust_id ,count(*) from orders,customers  where orders.cust_id=customers.cust_id ;
+---------+----------+
| cust_id | count(*) |
+---------+----------+
|   10001 |        5 |
+---------+----------+

Wy:为什么只返回一行记录

Bs:count(*) 是个聚集函数,而SQL语句并不涉及到分组子句,所以select执行完才执行count(*)  而为什么会返回10001而不是其他的,我猜想是与cust_id的属性有关auto_increment

 1.1>分组后的结果:

select customers.cust_id ,count(*) from orders,customers where orders.cust_id=customers.cust_id group by customers.cust_id;
+---------+----------+
| cust_id | count(*) |
+---------+----------+
|   10001 |        2 |
|   10003 |        1 |
|   10004 |        1 |
|   10005 |        1 |
+---------+----------+

2>

这两个SQL语句的不同主要在于()中的select子句的from

第一个涉及到外部查询,第二个不涉及

Wy:第二个SQL怎么不只返回一行,这与文章最前面的说法好像有些矛盾

Bs:自己的理解 第二个()是不涉及外部查询 所以orders只有一行 并且它先与第一个select执行完

 

以上内容纯属菜鸟理解,若有误欢迎指出!!!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值