简体   繁体   English

从 MySQL 查询中选择最后一行

[英]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).这不会满足我的目的,因为它首先将日期排序为 DESC(或 ASC)。

Suppose the qry_doc_date returns :假设 qry_doc_date 返回:

"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.然后它返回“2019-07-16”但我需要“2019-05-22”这是最后一行。

EDIT 1: I am trying to convert this VBA query to MYSQL.编辑 1:我正在尝试将此 VBA 查询转换为 MYSQL。

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

I suppose I misunderstood what the VBA query wants to return.我想我误解了 VBA 查询想要返回的内容。 Another issue is I do not have means to run this VBA query and check the result myself.另一个问题是我没有办法运行这个 VBA 查询并自己检查结果。

Your question doesn't make too much sense according to the SQL standard.根据 SQL 标准,您的问题没有太大意义。 In the absense of an ORDER BY clause the database engine is free to return the rows in any order.在没有ORDER BY子句的情况下,数据库引擎可以自由地以任何顺序返回行。 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.编辑 1:我正在尝试将此 VBA 查询转换为 MYSQL。

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

I suppose I misunderstood what the VBA query wants to return.我想我误解了 VBA 查询想要返回的内容。 Another issue is I do not have means to run this VBA query and check the result myself.另一个问题是我没有办法运行这个 VBA 查询并自己检查结果。

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.除非您的数据集qry_doc_date是通过order by子句order by ,否则DFirstDLast域聚合函数将基本上返回一个随机记录。

This is stated in the MS Access Documentation for these two functions:这在MS Access 文档中针对这两个功能进行了说明:

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.当您只需要该字段中的任何值时,您可以使用DFirstDLast函数从表或查询中的特定字段返回随机记录

[ ... ] [...]

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.如果要返回一组记录(域)中的第一条或最后一条记录,则应创建按升序或降序排序的查询,并将 TopValues 属性设置为 1。有关详细信息,请参阅 TopValues 属性主题。 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.从 Visual Basic for Applications (VBA) 模块,您还可以创建 ADO Recordset 对象并使用 MoveFirst 或 MoveLast 方法返回记录集中的第一条或最后一条记录。

What you need is to in qry_doc_date to include a sequential row number .您需要的是在qry_doc_date中包含一个连续的行号

Then you can use something like this:然后你可以使用这样的东西:

ORDER BY qry_doc_dates.row_number DESC LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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