1. Requirements: Obtain sales version combinations
Color (property name) + (black, white...) property value set
@Data
public static class ItemSaleAttrsVo{
private Long attrId;
private String attrName;
//当前属性有多少种版本:黑色,白色,蓝色,这里使用字符串拼接的方式所以没有使用集合存储
private String attrValues;
}
2. Group by grouping
Why do you often use group by to report errors? If you want to group successfully, you must ensure that the field values of the query results guarantee a one-to-one relationship
(1) Before grouping, the records of each field are in one-to-one correspondence
(2) select field A, field B from XX gorup by field A.
If only field A is group by, if it is grouped by category, then each Only one record will be displayed for each category. At this time, a one-to-many relationship will be formed with other ungrouped fields. Then
the database cannot display the results and will report an error
(3) How to solve the one-to-many problem and convert the ungrouped fields into It is also converted into a record, so that one-to-one can be guaranteed.
The query results can be divided into two categories, select the grouped fields, and the fields that do not participate in the grouping from xxx
use group by. The fields that do not participate in the grouping must use aggregation. Functions (group_concat, count, avg, sum...) aggregate into one record
3. Query results
Multiple records can be merged using the group_concat aggregation function
SELECT
b.attr_id,
b.attr_name,
GROUP_CONCAT(DISTINCT b.`attr_value`)
FROM `pms_sku_info` a LEFT JOIN `pms_sku_sale_attr_value` b ON a.sku_id = b.sku_id
WHERE a.spu_id = 28
GROUP BY b.attr_id,b.attr_name
distinct after deduplication