MYSQL:子查询创建字符字段

使用子查询的另一个方法是创建计算字段。假如需要显示Customers表中每个顾客的订单总数。订单与相应的顾客ID存储在Orders表中。
步骤如下:
1、从customers表中检索顾客列表;
2、对于检索出的每个顾客,统计其在Orders表中的订单数目。

SELECT cust_name, cust_state, (SELECT COUNT(*) 
FROM orders WHERE orders.cust_id = customers.cust_id ) AS orders
FROM customers
ORDER BY cust_name;

在这里插入图片描述

发布了19 篇原创文章 · 获赞 0 · 访问量 324

猜你喜欢

转载自blog.csdn.net/the_name_of_science/article/details/105401951