Mysql selects any one of two fields in relation to the value of the third field

I want to select a price or sale_price relative to a value in a field named nosale. where price, sale_price and nosale are the fields of the product table.

The nosale field will be either true or false. Accordingly, I want either the price value or sale_price, and not both.

How to form one request for extraction, as I mentioned?

+3
source share
1 answer
SELECT IF(nosale = 1, price, sale_price) AS `something` FROM table ...

or

SELECT CASE WHEN nosale = 1 THEN price ELSE sale_price END AS `something` FROM table ...

http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

+4
source

Source: https://habr.com/ru/post/1788150/


All Articles