mysql order by two fields equally

Posted on 2010-01-05
Last Modified: 2012-05-08

I currently have a mysql query set up with the order like this:

 ORDER BY LTpricetoday, LTafterrebates ASC, id DESC LIMIT 0, 20

I have added another product line, so now I need it to order by both LTpricetoday and BWpricetoday, so the ones with the smallest price today show up first - whichever it is.

Then after that, LTafterrebates would be the secondary order, id the last.

Does that make sense?  It is possible?

Thanks,  Chris
Question by:St_Aug_Beach_Bum
    LVL 32

    Expert Comment

    I think I understand. Can you post the actual SQL query you are using and a sample of what you have now and an sample (hand written probably) of what you want?

    Author Comment


    Well, currently I have it like this:

    "SELECT * FROM cps WHERE updated = '1262697422' AND style = 'PDA'  ORDER BY LTpricetoday, LTafterrebates ASC, id DESC LIMIT"

    but I need to add BWpricetoday into the ORDER section.

    So that the results will provide a combined list ordered first of all by comparing both LTpricetoday and BWpricetoday, so I might end up with this:

    BW - $0
    LT - $1
    LT - $1
    BW - $2
    LT - $3
    BW - $3
    LT - $4
    BW - $5
    LVL 7

    Expert Comment

    by:Vimal DM

    1)You can use like this
      query :  ORDER BY LTpricetoday ASC, LTafterrebates DESC, id DESC LIMIT 0, 20
    2)Even you can use the following example aslo
      query : ORDER BY 3 DESC LIMIT 0, 20
    3 - indicates the first three fields of the select query.


    Author Comment

    opps, just cut off the " DESC LIMIT 0, 20" of the previous query, but doesn't affect the question, still trying to figure out how to order by both fields on an equal level.
    LVL 5

    Accepted Solution

    Try the following,

          id DESC

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now