Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql select any one field out of two with respect to the value of a third field

Tags:

mysql

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

The nosale field will be either true or false. According to that i want either the value of price or sale_price and not both.

How to form a single query to fetch like i mentioned?

like image 363
Jayapal Chandran Avatar asked Sep 19 '25 15:09

Jayapal Chandran


1 Answers

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

like image 61
Dan Grossman Avatar answered Sep 22 '25 01:09

Dan Grossman