[英]Raw SQL query/django query : Get column info by grouping
Following are the tables: 下表是:
Product Table
25 Tshirt
26 Dress
ProductVariation Table
id product_id variation_id vendor_id
46 26 47 1
47 26 48 1
48 26 49 1
49 27 50 1
Variation Table
id value attribute_id category_id variationinfo_id
47 Female 2 1 17
48 89 3 1 17
49 90 1 1 18
50 #343434 2 1 18
VariationInfo Table
id stock remarks
17 8 remarks1
18 10 remarks2
Attribute Table
id attribute_name
1 size
2 color
3 gender
Category Table
id name parent remarks
1 bla bla bla
Problem scenario: Product 26 has variation_ids 47, 48, 49. Of these, 47 and 48 have same variationinfo_id 17 and another 49, has 18. For product 26, total stock information is obtained as 18. 问题场景:产品26具有variation_ids 47、48、49。其中,47和48具有相同的variationinfo_id 17,而另一个49具有48的variationinfo_id。对于产品26,获得的总库存信息为18。
However, I need to get distributed stock information for different variationinfo_ids, For example: For variation(VariationTable) 47, 48 with same variationinfo_id of 17, total stock of this variation -> 8 and for 48 with variationinfo_id of 18, total stock of this variation -> 10. 但是,我需要获取不同的variationinfo_ids的分布式库存信息,例如:对于variant(VariationTable)47、48,其variationinfo_id为17,此变化的总库存-> 8,对于48,其variationinfo_id为18,此总库存变化-> 10。
How do I write query for the latter part? 如何编写后半部分的查询?
select temp.product_id,temp.variation_ids,temp.variationinfo_id,vi.stock
(select p.product_id,group_concat(v.id) as variation_ids, v.variationinfo_id
from ProductVariation p
join Variation v on p.variation_id=v.id
group by p.product_id,v.variationinfo_id) temp
join VariationInfo vi on temp.variationinfo_id=vi.id
Note: Code is not tested. 注意:代码未经测试。 Small syntactic error may exist.
可能存在小的语法错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.