Solved

MYSQL sort minimum value of 3 fields

Posted on 2013-05-14
5
420 Views
Last Modified: 2013-05-29
Hi

I have 3 fields I want to sort by in MYSQL DB.

price1, price2, price3

I want to sort my result by testing each field and sorting by the minimum value of the 3

each will contain a value and they are in no order of low to high's
0
Comment
Question by:is_numeric
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39164466
sort by (price1 + price2 + price3)
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 125 total points
ID: 39164555
Sort by least(price1,price2,price3)
0
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 125 total points
ID: 39164635
I think that you'll need to create a column from the value of least() and sort by that.

  SELECT least (price1, price2, price3), {other columns}
  FROM xxx
  ORDER BY 1;
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 125 total points
ID: 39164753
it should not be necessary to add the calculation into the selection list, MySQL will permit sorting on calculations (and to be honest I do not like using column order as the sort method either - sometimes produces crazy unexpected results if code gets changed over time).

see:
http://sqlfiddle.com/#!9/f72c5/1

where, I prefer

order by (price1+price2+price3)

but that choice is entirely yours.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 125 total points
ID: 39166011
@PortletPaul, note the sort fails if a record has all low prices (e.g., 2, 5, 6), so the sum of prices is lowest even though individually each price is higher than the least of another row. see: http://sqlfiddle.com/#!9/05cb1/1

Therefore, I would combine both of the approaches. Nice work by the way Experts.
/* ... */
order by least(price1,price2,price3), (price1+price2+price3)
;

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question