无联结表的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执行完
以上内容纯属菜鸟理解,若有误欢迎指出!!!