我想在产品属性上结合我的postgres查询结果。目前它给了我这些结果:
name id value sku item_count
Item # 1 3 Item IT-EM1 3
Item # 1 2 006058465456 IT-EM1 3
Item # 2 3 Item IT-EM2 1
Item # 2 2 055045004505 IT-EM2 1
我希望它返回以下内容:
name id#1 value#1 id#2 value#2 sku item_count
Item # 1 3 Item 2 006058465456 IT-EM1 3
Item # 2 3 Item 2 055045004505 IT-EM2 1
id是产品属性id(2是GTIN,3是Brand,)值是该特定产品属性的值。我的查询如下:
SELECT
p.name,
l.property_id AS id,
l.value AS value,
v.sku,
s.count_on_hand AS item_count,
FROM
spree_variants v INNER JOIN
spree_products p ON v.product_id = p.id LEFT OUTER JOIN
spree_stock_items s ON v.id = s.variant_id INNER JOIN
spree_product_properties l ON l.product_id = p.id
WHERE
s.count_on_hand > 0
有什么想法吗?
答案 0 :(得分:0)
自己回答这个问题。也许不是最优雅的解决方案,但是:
SELECT
p.name,
l.property_id AS id,
l.value AS value,
li.property_id AS id_two,
li.value AS value_two,
v.sku,
s.count_on_hand AS item_count,
FROM
spree_variants v INNER JOIN
spree_products p ON v.product_id = p.id LEFT OUTER JOIN
spree_stock_items s ON v.id = s.variant_id INNER JOIN
spree_product_properties l ON l.product_id = p.id INNER JOIN
spree_product_properties li ON li.product_id = p.id
WHERE
s.count_on_hand > 0 AND
l.property_id = 2 AND
li.property_id = 3
再次将产品属性列添加为另一个变量(l和li),然后在WHERE中定义l.property_id = 2和li.property_id = 3