I'm using Sequelize.JS to connect to a MySQL database. I have the following table structure:
products
---------------------
id INT
name VARCHAR
price DECIMAL
price_special DECIMAL
I'd like to get the lowest price (or special_price, when available) and the highest price (or special_price when available). With raw SQL, I can do the following:
SELECT
MIN(IF(`price_special` > 0, `price_special`, `price`)) AS `min`,
MAX(IF(`price_special` > 0, `price_special`, `price`)) as `max`
FROM
`products`
;
I also know I can select a column wrapped by an SQL function in Sequelize, this way:
product.findOne({
attributes: [
[ Sequelize.fn('MIN', Sequelize.col('price')), 'min' ],
[ Sequelize.fn('MAX', Sequelize.col('price')), 'max' ],
]
});
However, I don't know how to to nest MIN and IF functions, the way I do with raw SQL.