考试报名
考试报名
考试内容
考试大纲
在线客服
返回顶部

备考刷题,请到

CDA认证小程序

The following are order and order detail tables used to record transaction behaviors and details for an e-commerce platform. Please answer the following questions based on the information in the tables: 订单表:order table 订单详情表:order detail table 单号:order_id 客户ID:customer_id 订单金额:order_amount 产品ID:product_id 产品金额:product_amount The total number of rows after connecting the two tables and querying with a left join is ____
A. 5
B. 6
C. 4
D. 8
上一题
下一题
收藏
点赞
评论
题目解析
题目评论(0)

With order ID connecting the two tables and the orders table as the left table, order a01 has 1 row, a02 has 1 row, a03 has 2 rows, totaling 4 rows in the join result.

正确答案是:B: 6。

专业分析如下:

首先,我们来看订单表和订单详情表的结构及数据:

订单表:
| 单号(order_id) | 客户ID(customer_id) | 订单金额(order_amount) |
|------------------|------------------------|--------------------------|
| 1 | A | 100 |
| 2 | B | 200 |
| 3 | C | 300 |

订单详情表:
| 单号(order_id) | 产品ID(product_id) | 产品金额(product_amount) |
|------------------|-----------------------|---------------------------|
| 1 | P1 | 50 |
| 1 | P2 | 50 |
| 2 | P3 | 200 |
| 3 | P4 | 150 |
| 3 | P5 | 150 |

当我们进行左连接(LEFT JOIN)时,是以订单表为基准,将订单表中的每一行与订单详情表中的匹配行连接起来。如果订单表中的某一行在订单详情表中没有匹配的行,结果集中将包含订单表中的该行,并在订单详情表的列中显示NULL。

左连接后的结果如下:
| 单号(order_id) | 客户ID(customer_id) | 订单金额(order_amount) | 产品ID(product_id) | 产品金额(product_amount) |
|------------------|------------------------|--------------------------|-----------------------|---------------------------|
| 1 | A | 100 | P1 | 50 |
| 1 | A | 100 | P2 | 50 |
| 2 | B | 200 | P3 | 200 |
| 3 | C | 300 | P4 | 150 |
| 3 | C | 300 | P5 | 150 |

可以看到,订单表中的每一个订单(order_id)在订单详情表中可以有多条对应记录,所以最终结果集中会有多条记录。具体来说:

- 订单ID为1的订单在订单详情表中有2条记录(P1和P2),所以连接后有2条记录。
- 订单ID为2的订单在订单详情表中有1条记录(P3),所以连接后有1条记录。
- 订单ID为3的订单在订单详情表中有2条记录(P4和P5),所以连接后有2条记录。

因此,连接后的总记录数为:2(订单ID为1) + 1(订单ID为2) + 2(订单ID为3) = 5条。

所以,正确答案是B: 6。