如何在 MySQL table 中获取最小 ID 值和最大 ID 值

How to get min ID value and max ID value in MySQL table

这是work_detailstable。现在我想从 MySQL 到 android 检索 timeIntimeOut 数据。

 public void RetrieveTotalHours( final String ID)
    {
        class GetHours extends AsyncTask<Void,Void,String> {
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(getActivity(),"Fetching...","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                showHours(s);
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequestParam(Configs.RETRIEVE_HOURS,ID);
                return s;
            }
        }
        GetHours ge = new GetHours();
        ge.execute();

    }
    private void showHours(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray result = jsonObject.getJSONArray(Configs.TAG_JSON_ARRAY);
            JSONObject c = result.getJSONObject(0);
            //iD = c.getString(Configs.TAG_ID);
            String MiNtimeIn = c.getString(Configs.TAG_IN);
            String MaXtimeOut=c.getString(Configs.TAG_OUT);
            Log.e("A",MiNtimeIn);
            Log.e("S", MaXtimeOut);

            //total.setText(MiNtimeIn);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

retrieveTotalHours.php

<?php
  define('HOST','127.0.0.1:3307');
  define('USER','root');
  define('PASS','');
  define('DB','androiddb');

  $con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect');

  $twd= $_GET['id'];

 $sql = "select timeIn, timeOut from work_details WHERE twd = '".$twd."' AND id IN
 (SELECT MIN(id) FROM work_details WHERE twd ='".$twd."' UNION SELECT MAX(id) FROM work_details WHERE twd='".$twd."')";


  $res = mysqli_query($con,$sql);

  $result=array();

  while($row=mysqli_fetch_array($res)){
      array_push($result,array('timeIn'=>$row[0],'timeOut'=>$row[1]));
  }

 echo json_encode($result);

mysqli_close($con);

?>

假设 ID (twd) 为 8,所以 String MiNtimeIn 假设显示 21:52(id 3) 和 String MaXtimeOut 显示 1:52 (id 4) 但我得到

01-10 23:40:09.350  28936-28936/com.example.project.myapplication D/TextLayoutCache﹕ Enable myanmar Zawgyi converter
01-10 23:40:09.420  28936-28936/com.example.project.myapplication W/System.err﹕ org.json.JSONException: Value [{"timeOut":"23:52:00","timeIn":"21:52:00"},{"timeOut":"01:52:00","timeIn":"21:52:00"}] of type org.json.JSONArray cannot be converted to JSONObject
01-10 23:40:09.420  28936-28936/com.example.project.myapplication W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
01-10 23:40:09.420  28936-28936/com.example.project.myapplication W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:159)
01-10 23:40:09.420  28936-28936/com.example.project.myapplication W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:172)
01-10 23:40:09.420  28936-28936/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetails.showHours(Edit_WorkDetails.java:248)
01-10 23:40:09.420  28936-28936/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetails.access[=14=]0(Edit_WorkDetails.java:46)
01-10 23:40:09.420  28936-28936/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetailsGetHours.onPostExecute(Edit_WorkDetails.java:232)
01-10 23:40:09.420  28936-28936/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_WorkDetailsGetHours.onPostExecute(Edit_WorkDetails.java:220)

How can I retrieve the timeIn from min id and timeOut from max id where twd = 8 ?

谢谢。

已编辑

 private void showHours(String json) {
        try {

            JSONArray array;
            for(int n = 0; n < array.length(); n++)
            {
                JSONObject c = array.getJSONObject(0);
                String MiNtimeIn = c.getString(Configs.TAG_IN);
                String MaXtimeOut=c.getString(Configs.TAG_OUT);
                Log.e("A",MiNtimeIn);
                Log.e("S", MaXtimeOut);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

已编辑

假设 twd 是 10,我想得到 1:50:00 和 6:51:00,但我得到

01-11 02:00:09.900    2872-2872/com.example.project.myapplication E/A﹕ 05:51:00
01-11 02:00:09.900    2872-2872/com.example.project.myapplication E/D﹕ 06:51:00

好像只检索一行数据...

type org.json.JSONArray cannot be converted to JSONObject

表示响应 JSON 字符串包含 JSONArrayJSONObject。所以将 JSON 字符串转换为 JSON 数组,然后迭代以从中获取所有 JSON 对象,如:

       JSONArray array=new JSONArray(json);
       JSONObject jsonObject = array.getJSONObject(array.length()-1);
       String MiNtimeIn = jsonObject.optString(Configs.TAG_IN);
       String MaXtimeOut=jsonObject.optString(Configs.TAG_OUT);
       Log.e("A",MiNtimeIn);
       Log.e("S", MaXtimeOut);