简体   繁体   中英

Select last row from a MySQL query

I have a query that returns some dates which are not in any order. I need to select the last row from the sub query. The problem is all the solutions I can find online uses something like

ORDER BY qry_doc_dates.arrival_date DESC LIMIT 1

Select qry_doc_dates.arrival_date 
FROM (qry_doc_date) AS qry_doc_dates 
ORDER BY qry_doc_dates.arrival_date DESC 
LIMIT 1

which will not serve my purpose because it first orders the dates as DESC(or ASC).

Suppose the qry_doc_date returns :

"2019-05-27",
"2019-05-13",
"2019-05-20",
"2019-05-22",
"2019-07-12",
"2019-05-22",
"2019-07-16",
"2019-05-22"

As we can see that the returned values are not in order. If I use

ORDER BY qry_doc_dates.arrival_date DESC LIMIT 1

then it returns "2019-07-16" But I need "2019-05-22" which is the last row.

EDIT 1: I am trying to convert this VBA query to MYSQL.

DLast("arrival_date", "qry_doc_date", "[package_id] = " & Me!lstPackage)

I suppose I misunderstood what the VBA query wants to return. Another issue is I do not have means to run this VBA query and check the result myself.

Your question doesn't make too much sense according to the SQL standard. In the absense of an ORDER BY clause the database engine is free to return the rows in any order. This order may even change over time.

So essentially you are requesting the "last random row" the query returns . If this is the case, why don't you get the "first random row"? It doesn't make any difference, does it?

The only way of getting the last random row is to get them all and discard all of them except for the last one.

Now, if you just need one random row , I would suggest you just get the first random row, and problem solved.

In response to the additional information from your edit:

EDIT 1: I am trying to convert this VBA query to MYSQL.

 DLast("arrival_date", "qry_doc_date", "[package_id] = " & Me!lstPackage)

I suppose I misunderstood what the VBA query wants to return. Another issue is I do not have means to run this VBA query and check the result myself.

Unless your dataset qry_doc_date is ordered by means of an order by clause, the DFirst or DLast domain aggregate functions will return essentially a random record.

This is stated in the MS Access Documentation for these two functions:

You can use the DFirst and DLast functions to return a random record from a particular field in a table or query when you simply need any value from that field.

[ ... ]

If you want to return the first or last record in a set of records (a domain), you should create a query sorted as either ascending or descending and set the TopValues property to 1. For more information, see the TopValues property topic. From a Visual Basic for Applications (VBA) module, you can also create an ADO Recordset object and use the MoveFirst or MoveLast method to return the first or last record in a set of records.

What you need is to in qry_doc_date to include a sequential row number .

Then you can use something like this:

ORDER BY qry_doc_dates.row_number DESC LIMIT 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM