Question

For example I have 2 tables:

product:
id: integer
price: integer

variation:
id: integer
product_id: integer
price integer

Some products have variations, some products don't have variations. I want to get all products, sorted by price. And if product has variation, this product must be sorted by first variation price, not its own price.

And is it possible to make such Propel Criteria or Query ?

Was it helpful?

Solution

SELECT
*
FROM
product p
LEFT JOIN variation v ON p.id = v.product_id
ORDER BY COALESCE(v.price, p.price)

COALESCE() returns the first of its parameters which isn't NULL. The LEFT JOIN returns everything from product table, if there's no match on variation table, there's NULL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top