【问题标题】:Combining two results结合两个结果
【发布时间】:2015-05-18 14:38:37
【问题描述】:

我想在产品属性中合并我的 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 是品牌),值是该特定产品属性的值。我的查询如下:

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

有什么想法吗?

【问题讨论】:

  • 请提供您的 Postgres 版本(总是)。表定义也很有帮助——你在 psql 或有效的 DDL 脚本中使用\d tbl 得到的东西——只是相关的列,但有所有约束。结果中的id 是否只有两个不同的值或更多?

标签: postgresql spree


【解决方案1】:

我自己回答了这个问题。也许不是最优雅的解决方案,但是:

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

【讨论】:

    猜你喜欢
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多