Postgresql 结合两个结果
我想在product properties中合并postgres查询的结果。目前它给了我这些结果:Postgresql 结合两个结果,postgresql,spree,Postgresql,Spree,我想在product properties中合并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 I
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是品牌),值是该特定产品属性的值。我的问题如下:
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
有什么想法吗?我自己回答了这个问题。也许不是最优雅的解决方案,但是:
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
再次将product properties列添加为不同的变量(l和li),然后在其中定义l.property_id=2和li.property_id=3请提供您的Postgres版本(一如既往)。表定义也会很有帮助—在psql或有效的DDL脚本中使用
\d tbl
可以得到什么—只是相关的列,但有所有约束。结果中的id
是否只有两个或更多不同的值?