svk
svk

Reputation: 4553

How to make the Order by statement for Two fields in a single query and one field needs to DESC and one field needs to ASC using Mysql?

I want to make a query that will give the result on order by using two columns. I made query like this.

select 
    el.*,
    lb.LabName,lb.LabType,
    lb.LabDescription
from encounterlab el
INNER JOIN
    labs lb
    ON 
    lb.LabType=el.LabType
where 
    PatientAcctNo=4 ORDER BY el.DataOfService,lb.LabName DESC

It gives the results.But My aim is IF in the DataOfService contain the same date I want to make the Order depending upon the LabName and this should be ASC

Upvotes: 0

Views: 623

Answers (2)

Blixt
Blixt

Reputation: 50197

If I understand you correctly, your current query orders in the opposite direction that you want (for both columns!). Ordering ascending (ASC) is implicit, and you always have a direction per column you're ordering by. So your example:

ORDER BY el.DataOfService,lb.LabName DESC

Is the same as:

ORDER BY el.DataOfService ASC, lb.LabName DESC

So what you want is this:

ORDER BY el.DataOfService DESC, lb.LabName

Upvotes: 0

Andreas Linden
Andreas Linden

Reputation: 12721

you can optionally specify the order for each single field in the order by statement

ORDER BY field1 DESC, field2 ASC

Upvotes: 3

Related Questions