Skip to main content
 首页 » 编程设计

mysql之MyBatis 数据映射。收藏。关系表

2023年09月26日84qlqwjy

基金分配模板

template_id | 模板名称 | 状态 |

1 大乐透 1

2 EZ-2 1

fund_allocation_item

|项目 ID |项目名称 |状态|

1 慈善 1

2 运营费用1

3 食物津贴 1

4 交通补贴1

| 5 |灾难受害者| 1 |

r_template_item

| template_id | item_id | item_percentage |

1 1 45.00

1 2 55.00

2 1 40.00

2 2 46.00

//FundAllocationTemplate.java(domain) 
private Integer templateID; 
private String templateName; 
private Integer templateStatus 
 
//FundAllocationItem.java(domain) 
private Integer itemID; 
private String itemName; 
private Integer itemStatus 
 
<resultMap id="fundTemplate" type="FundAllocationTemplate"> 
    <id property="fundTemplateID"           column="template_id" /> 
    <result property="fundTemplateName"         column="template_name" /> 
    <result property="fundTemplateStatus"       column="status" /> 
    //Association for Status 
</resultMap> 
 
<resultMap id="fundItem" type="FundAllocationItem"> 
    <id property="fundItemID"       column="item_id" /> 
    <result property="fundItemName"     column="item_name" /> 
    <result property="fundItemStatus"   column="status" /> 
    //Association for Status 
</resultMap> 
 
FORM 
private Integer templateID; 
private String templateName; 
private BigDecimal totalPercentage; 
private Integer status; 

表单或页面输出

|分配模板名称 |基金模板百分比|状态 |

| super 乐透 | 100% |活跃|

| EZ-2 | 86% |活跃 |

问题:如何才能显示具有给定域和表格设计的每个基金模板的总百分比?

请您参考如下方法:

使用以下查询:

select T.template_name,SUM(R.item_percentage) as Fund_Template_Percentage,  
(CASE T.status WHEN 1 THEN "Active" ELSE "NOT Active" END) STATUS  
from fund_allocation_template T, r_template_item R  
where T.template_id=R.template_id 
GROUP BY R.template_id;