解决问题:
按照一定条件进行查询,发现结果中属性相同,属性对应的值有一部分不同。把相同值只保留一个,不同部分放到数组里,这时我们就用到了聚合函数。
实例:
解释:
<select id="GoodsMessage" resultType="java.util.Map">
SELECT
u.address,
s.avatarUrl,
s.price,
s.name,
s.description,
u.username,
JSON_ARRAYAGG(c.GoodsCode) AS goodsCode,
JSON_ARRAYAGG(c.user_comment) AS userComment,
//一个属性中,有多个值,将这多个值存入一个数组之中,并将这个数组重新命名
COUNT(c.GoodsCode) AS goodsCodeCount,
COUNT(c.user_comment) AS userCommentCount
//记录查寻到属性相同元素值却不相同的元素个数
FROM
shop_message s
INNER JOIN
comment c ON c.code = s.code
INNER JOIN
user u ON u.phonenumber = s.phone
//将三张表进行连接起来
GROUP BY
u.address,
s.avatarUrl,
s.price,
s.name,
s.description,
u.username;
//让上面属性相同,值相同分为一组
</select>