这道题目要求我们查询出产生理赔金额的快递信息,我们要做的事情如下:

1. 确定总体问题

我们需要找出所有产生理赔金额的快递信息,输出快递单号、快递种类和理赔费用,并按理赔费用降序排序。

2. 分析关键问题

  • 连接表:将express_tbexp_cost_tb表连接起来,以便获取每个快递的费用信息。
  • 筛选有理赔费用的快递:筛选出理赔费用不为NULL的快递。
  • 排序输出:按理赔费用降序排序。

3. 解决每个关键问题的代码及讲解

步骤1:连接表

我们使用JOINexpress_tbexp_cost_tb表连接起来:

from
    express_tb e
    join exp_cost_tb c on e.exp_number = c.exp_number
  • JOIN exp_cost_tb c ON e.exp_number = c.exp_number:通过快递单号连接两个表,以便获取每个快递的费用信息。
步骤2:筛选有理赔费用的快递

我们使用WHERE子句筛选出理赔费用不为NULL的快递:

where
    c.claims_cost is not null
  • c.claims_cost IS NOT NULL:筛选出有理赔费用的快递。
步骤3:排序输出

我们使用ORDER BY按理赔费用降序排序输出结果:

order by
    c.claims_cost desc

完整代码

select
    e.exp_number,
    e.exp_type,
    c.claims_cost
from
    express_tb e
    join exp_cost_tb c on e.exp_number = c.exp_number
where
    c.claims_cost is not null
order by
    c.claims_cost desc;