一对多查询转行为列
问题:为什么要转行为列?
回答:MyBatis查询一对多数据展示分页时,ResultMapper配置使用一对多集合标签(<conllection></collection>)会影响分页
使用WM_CONCAT()函数,转行为列查询
示例如下:
0.页面展示
1.模型表数据
SELECT t.I FROM Score_Model t WHERE t.id = 553;
2.模型角色关联表数据
SELECT * FROM SCORE_Model_Rule t2 WHERE t2.model_id = 553;
3.角色表数据
SELECT t3.id,t3.role_name FROM Sys_Auth_Role t3 WHERE t3.id IN (41,47,49,67);
4.一对多查询
SELECT t1.id,t2.name,t3.id,t3.role_name FROM Score_Model t1
LEFT JOIN Score_Model_Rule t2 ON t1.id = t2.model_id
LEFT JOIN Sys_Auth_Role t3 ON t3.id = t2.role_id
WHERE t1.id = 553
5.一对多转行为列查询(其他的列都要在group by字句中)
SELECT t1.id,t2.name,wm_concat(t3.id),wm_concat(t3.role_name) FROM Score_Model t1
LEFT JOIN Score_Model_Rule t2 ON t1.id = t2.model_id
LEFT JOIN Sys_Auth_Role t3 ON t3.id = t2.role_id
WHERE t1.id = 553
GROUP BY t1.id,t1.name