查询一个列和一个计算,列在相同的时间PostgreSQL
-
19-09-2019 - |
题
我有两个表, Products
和 BundleProducts
有o2o关系BaseProducts.一个 BundleProduct
是的集合 Products
使用m2m关系 Products
表。Products
有一个 price
柱和价格的 BundleProduct
是总数来计算的价格 Products
.
BaseProducts
有列像 name
和 description
所以我可以查询这得到两个 Products
和 BundleProducts
.
是否有可能查询和 sort by price
两对 price
列 Products
和计算 price
的 BundleProducts
?
解决方案
试试这样的事情:
SELECT name, description, price
FROM (
SELECT name, description, price FROM products
UNION
SELECT bundle_products.name, bundle_products.description, sum(products.price)
FROM bundle_products
JOIN products on (<your join condition)
GROUP BY bundle_products.name, bundle_products.description
) AS combined
ORDER BY price
不隶属于 StackOverflow