How to make order by desc without giving limits


#1

Dear All,

I am trying to filder the records using order by desc but it is not working (for looping)

I tried GROUP BY emi_flag.booking_id1 DESC ORDER BY emi_flag.flag_id DESC but it shows first value only

this is my code

SELECT * FROM emi_flag
INNER JOIN booking_master ON emi_flag.booking_id1=booking_master.booking_id
WHERE emi_flag.booking_id1=104

OUTPUT

emi_id    booking_id1    pay_date

1               22                  10-11-2018
2               23                   8-11-2018
3               22                   20-11-2018
4               22                   25-11-2018

I want Output like:

emi_id       booking_id1        pay_date

1                 23                     18-11-2018

2                  22                     25-11-2018

I want Output like:

any Suggestions please…


#2

Firstly, nothing in your post matches up or accurately defines what result you are trying to get and why. That query cannot be producing the OUTPUT you show, because you are searching for emi_flag.booking_id1=104. There cannot be booking_id1 values of 22 and 23 in the result. There’s also no way that the emi_id values in the desired output can be 1 and 2 for booking_id1 values of 23 and 22 respectively (I can believe they should be 2 and 4, based on what I think you are trying to accomplish.)

Don’t use SELECT *, You need to select just the columns that you want. This is additionally important in JOINed queries, since it helps to document what you are trying to do, i.e. which columns you are selecting from which table.

Don’t have column(s) ending in numbers. If you have a sequence of such columns, i.e. booking_id1, 2, 3, … that indicates a bad database design. The booking_id1 column should just be named booking_id.

You should use table aliases in your query to cut down on the clutter and typing you have to do.

You should store dates in a DATE data type column, in which case they will have the format - YYYY-MM-DD.

Lastly, since you didn’t accurately state/show what result you want and why, I’ll take a guess that you want the last row (highest emi_id value) within each group of booking_id values? If this is correct, see this link - https://dev.mysql.com/doc/refman/8.0/en/example-maximum-column-group-row.html


#3

can u please suggest me what query i will wrote to get the above output…?

i tried like but it take first value (asc)

SELECT * FROM emi_flag GROUP BY booking_id1 DESC


#4

I Got the Solution

thank you