我正在建立一个销售鞋子的网上商店。我正在尝试编写一条命令,以显示1个特定运动鞋的每种尺寸的数量。
这是我的ER图
Link to my ER-Diagram can be seen here: imgur.com/a/QHpid2p
我想将数量(qty)添加到所有不同的尺寸,但是我尝试的所有方法似乎都无效。
SELECT sn.sneaker_id, sn.sneaker_name,
(CASE WHEN sn.gender = 0 THEN 'Women'
WHEN sn.gender = 1 THEN 'Men'
END) as gender,
sn.description, sn.price,
GROUP_CONCAT(si.size),
GROUP_CONCAT(DISTINCT(i.path)),
bn.brand_name
FROM sneakers sn JOIN
sizes si
ON si.sneaker_fk = sn.sneaker_id JOIN
images i
ON i.sneaker_fk = sn.sneaker_id JOIN
brand_names bn
ON bn.brand_name_id = sn.brand_name_fk
WHERE sn.sneaker_id = 1
这是我的实际结果:
sneaker_id : 1
sneaker_name : Air Max 97 Silver Bullet
gender : Men
description : anfaslknsaf
price : 1500
GROUP_CONCAT(sizes.size) : 41.0,43.0,44.0,45.5,46.0
GROUP_CONCAT(DISTINCT(images.path)): img
brand_name : nike
我想要的输出如下:
sneaker_id : 1
sneaker_name : Air Max 97 Silver Bullet
gender : Men
description : anfaslknsaf
price : 1500
GROUP_CONCAT(sizes.size) : [41.0, 10],[43.0, 5],[44.0, 8],[45.5, 7],[46.0,8]
GROUP_CONCAT(DISTINCT(images.path)): img
brand_name : nike
答案 0 :(得分:0)
您需要手动连接两列:
GROUP_CONCAT(CONCAT('[', sizes.size, ', ', sizes.qty, ']'))
答案 1 :(得分:0)
优良作法是按GROUP BY
查询中的所有未聚合列进行聚合。
GROUP_CONCAT()
接受多个参数并将它们串联在一起。因此,您可以这样做:
SELECT sn.sneaker_id, sn.sneaker_name,
(CASE WHEN sn.gender = 0 THEN 'Women'
WHEN sn.gender = 1 THEN 'Men'
END) as gender,
sn.description, sn.price,
GROUP_CONCAT('[', si.size, ', ', si.qty, ']'),
GROUP_CONCAT(DISTINCT(i.path)),
bn.brand_name
FROM sneakers sn JOIN
sizes si
ON si.sneaker_fk = sn.sneaker_id JOIN
images i
ON i.sneaker_fk = sn.sneaker_id JOIN
brand_names bn
ON bn.brand_name_id = sn.brand_name_fk
WHERE sn.sneaker_id = 1
GROUP BY sn.sneaker_id, sn.sneaker_name, gender, sn.description, sn.price, bn.brand_name;
请注意,您可以删除WHERE
子句,这将适用于所有运动鞋ID。