3

I want to search min value from four fields in mysql query, I have tried a lot of ways but didn't get it, right now I am searching only from one field,

here are my fields.

Table :packages

package_id        price   price_double    price_triple    price_quad

I want to search the min amount from four fields

like in php function

 min(price,price_double,price_triple,price_quad).

but I want to implement it in mysql query how can this be possible? please help me. thanks in advance.

Þaw
  • 2,047
  • 4
  • 22
  • 39
usii
  • 1,113
  • 1
  • 10
  • 17

3 Answers3

4

Use LEAST:

SELECT
    package_id,
    LEAST(price, price_double, price_triple, price_quad) AS price
FROM package;
alexn
  • 57,867
  • 14
  • 111
  • 145
Dharmesh Patel
  • 1,881
  • 1
  • 11
  • 12
0
select     case when price <= price_double and price <= price_triple and price<=price_quad then price
           when price_double <= price and price_double <= price_triple and price_double <=price_quad then price_double
          when price_triple<=price and price_triple<=price_double and pricetriple <= price_quad then price_triple
           when price_quad <= price and price_quad <= price_double and price_quad <=price_triple then price_quad
    end as 'TheMin'             

from Table packages

If you have any doubt please check this stackoverflow question link.

Community
  • 1
  • 1
Venkata Krishna
  • 4,287
  • 6
  • 30
  • 53
0

what you think about that? is it right?

   SELECT LEAST(MIN(price), MIN(price_double), MIN(price_triple), MIN(price_quad))
   AS MinOfAllColumns
   FROM packages
usii
  • 1,113
  • 1
  • 10
  • 17