简体   繁体   English

如何 select 数据库中的第一行/行

[英]How to select the first row/line from database

I am trying to make a java program program can takes just one row/line from phpMyAdmin database.我正在尝试制作一个 java 程序程序可以只从 phpMyAdmin 数据库中获取一行/行。 what I mean is in the photo我的意思是在照片中在此处输入图像描述

The problem is that I couldnt figure out how to take just the first line because its always keep giving me all the id'S, dafat's, etc. Is there any way I can get every line alone or even every data alone (jsut 1 id from the first line for example).问题是我不知道如何只取第一行,因为它总是不断给我所有的 id'S,dafat's 等。有什么办法可以单独获取每一行甚至每个数据(jsut 1 id 来自例如第一行)。 I would appreciate who can help me with this.我很感激谁能帮助我。

 Connection con = myConnection.getconnection();
    try{
        PreparedStatement ps = con.prepareStatement("SELECT `id`, `dafat`, `sinif`, `adet`, `price`, `type`, `total` FROM "+ff1+"  WHERE 1 ");
        ResultSet resultset = ps.executeQuery();
        System.out.println(resultset.getString("id"));


    }
    catch (Exception e) {

    }

Use the LIMIT clause:使用LIMIT子句:

PreparedStatement ps = con.prepareStatement(
  "SELECT `id`, `dafat`, `sinif`, `adet`, `price`, `type`, `total` FROM "
    + ff1 + " LIMIT 1");

If you want the first id value (the smallest one) you can combine with ORDER BY , as in:如果您想要第一个id值(最小的),您可以与ORDER BY结合使用,如下所示:

PreparedStatement ps = con.prepareStatement(
  "SELECT `id`, `dafat`, `sinif`, `adet`, `price`, `type`, `total` FROM "
    + ff1 + " ORDER BY id LIMIT 1");

Replace WHERE whit LIMIT将 WHERE 替换为 LIMIT

PreparedStatement ps = con.prepareStatement("SELECT `id`, `dafat`, `sinif`, `adet`, `price`, `type`, `total` FROM "+ff1+" ORDER BY id  LIMIT 1 ");

Use Rownum in your where clause like:在 where 子句中使用 Rownum,例如:

"SELECT * FROM all_objects WHERE rownum < 2"

https://mfaisal1521.blogspot.com/2020/04/rownum-in-sql.html https://mfaisal1521.blogspot.com/2020/04/rownum-in-sql.html

And also resultset.getString("id") start fetch result from first index of query untill we dont move our cursor to next resultset.并且 resultset.getString("id") 开始从查询的第一个索引获取结果,直到我们不将 cursor 移动到下一个结果集。

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

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