Exyu Portal
Exyu Portal

Reputation: 109

mysql select lowest price from multi select

TABLE price

user_id    b01    b02    b03    b04    b05    b06    b07    b08    b09
MP01       21     32     12     34     56     26     21     21     26    
MO11       81     332    112    1      12     22     71     17     23  

How to SELECT lowest price FROM price WHERE user_id = 'MP01' ?

example for user_id MP01, to get the result 12

Upvotes: 1

Views: 83

Answers (4)

Manil Liyanage
Manil Liyanage

Reputation: 255

This is an alternative approach to LEAST. Not easy as Using Least function. But might come in handy in some situations

SELECT MIN(b01) FROM(
select user_id , b01 from price
union all
select user_id , b02 from price
union all
select user_id , b03 from price
union all
select user_id , b04 from price
union all
select user_id , b05 from price
union all
select user_id , b06 from price
union all
select user_id , b07 from price
union all
select user_id , b08 from price
union all
select user_id , b09 from price
) temp 
WHERE user_id = 'MP01'

Upvotes: 1

Trent
Trent

Reputation: 86

you can use the LEAST function, as in:

select least(b01,b02,b03,b04,b05,b06,b07,b08,b09) from price where user_id = 'MP01'

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_least

Upvotes: 0

radar
radar

Reputation: 13425

you can use least

select least(b01,b02,b03,b04,b05,b06,b07,b08,b09)
FROM Table1
where user_id='MP01'

Upvotes: 0

Steve
Steve

Reputation: 11963

Base on your example i think you mean the result is 12. If that is the case you can do

SELECT LEAST(b01, b02, b03, b04, b05, b06, b07, b08, b09) FROM price WHERE user_id = 'MP01'

Upvotes: 2

Related Questions