简体   繁体   English

如何选择MySQL表的最后一行? 我没有任何自动增量键

[英]How can I SELECT the last row in a MySQL table ? I don't have any auto increment key

I want to SELECT the last row in a MySQL table. 我想在MySQL表中选择最后一行。 I don't have any auto increment key in that table. 我在该表中没有任何自动增量键。

public String getLastLeaveId() {
    try {
        Statement stmt  = getConnection();
        String sql="select leaveID from applied leave"; // here I want some logic that will fetch only leave_id from last row only
        stmt.execute(sql);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}


//Table Structure --->
(leave_id varchar(50) primary key,emp_id varchar(40),birthday varchar(20),anniv varchar(20),rh varchar(200),total int,foriegn key(emp_id) reference employee);

降序排列在您现在要订购的任何列上,最后limit 0,1

There is no implicit ordering in a database table. 数据库表中没有隐式排序。 If you don't have an auto-increment column, a date/time column, or some other column that follows a natural order when you insert rows, you're limited to using some other way of faking it. 如果在插入行时没有自动递增的列,日期/时间列或其他遵循自然顺序的列,则只能使用其他伪造方法。

You don't show a table structure in your question, nor do you state whether you're using MyISAM or InnoDB. 您没有在问题中显示表结构,也没有说明您使用的是MyISAM还是InnoDB。 MyISAM stores rows in order they're inserted (usually) so you can use something inneficient like ResultSet.last() from Java to get the last row, but if you're not using MyISAM you can't depend on that. MyISAM按通常的顺序存储行,因此您可以使用Java中的ResultSet.last()类的无效函数来获取最后一行,但是如果您不使用MyISAM,则不能依赖于此。 InnoDB, the default storage engine, stores records in a more efficient B-Tree order, and does not retain the insert order at all, but rather stores records according to the primary key's order. InnoDB是默认的存储引擎,它以更高效的B树顺序存储记录,并且根本不保留插入顺序,而是根据主键的顺序存储记录。

In short, you'll need to pick a field that has some natural order (or invent one) and order by that. 简而言之,您需要选择一个具有自然顺序(或发明一个自然顺序)的字段,并以此顺序进行排序。

it's may be useful 这可能有用

   SELECT MAX(ID) FROM YourTable

   // Similarly, ORDER BY with LIMIT:

    SELECT ID FROM YourTable ORDER BY ID Desc Limit 1

Without having either a where clause and/or order by condition you can't fetch the last row in a query directly. 如果没有条件子句和/或条件,您将无法直接获取查询的最后一行。 You need to traverse through all the rows to stop before last row. 您需要遍历所有行才能在最后一行之前停止。

Only positive options I see are: 我看到的唯一积极选择是:

  1. Select all rows. 选择所有行。 execute a rs.last() and then read it. 执行rs.last()然后读取它。
  2. Use order by desc clause based on a column. 基于列使用order by desc子句。
  3. Define a virtual row_num on all selected rows and run a select with desc order on that row_num. 在所有选定的行上定义一个虚拟的row_num ,并在该row_num上以desc顺序运行选择。
  4. Execute two statements. 执行两个语句。 One to find row count and the second to limit to fetch last row. 一个用于查找行数,第二个用于限制以获取最后一行。

Pseudo example on 4th option above: 上面第4个选项的伪示例:

  • int rowCount = result from "Select count(*) from my_table"; int rowCount =来自“从my_table中选择count(*)”的结果;
  • String query = "select * from my_table limit " + ( rowCount - 1 ) + ", 1"; 字符串查询=“从my_table限制中选择*” +(rowCount-1)+“,1”;
  • rs = stmt.execute above query. rs = stmt.execute以上查询。
  • read the only record, none but the last record, from rs, fetched. 从rs读取唯一的记录,但最后一个记录则是最后一个记录。

这就是你想要的。

SELECT * FROM table_name ORDER BY id DESC LIMIT 1

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

相关问题 MySQL 表错误中的自动增量键 - MySQL auto increment key in table mistakes Android Studio(SQLite 数据库),我要在表中添加一个 LineNo,我怎样才能让它自动递增 1? - Android Studio (SQLite Database), I am gonna to add a LineNo to a table and how can I make it will auto increment by 1? 没有任何实例变量时可以使用静态方法吗? - Can I use static methods when I don't have any instance variables? 我无法理解没有父母的孩子的投射界面 - I can't understand casting interface to children that don't have any parents 如何自动增加MySQL表中的列? - How to auto-increment a column in a MySQL table? 如何在 Java 中为自动增量表创建查询? - How do I create a query in Java for auto increment table? 我只是以某种方式破坏了Netbeans,但是我不知道如何解决它 - I just somehow broke Netbeans, and I don't have any clue how to fix it 即使我没有任何实现,我如何能够引用 Entity Manager 实例? - How am I able to reference Entity Manager instance even if I don't have any implementation? 我没有任何错误,无法将新分类器添加到weka - can not add new classifier to weka although I don't have any error 如何为Java创建或编码以自动递增电子邮件 - How can I create or code to auto increment email, for java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM