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

时间:2016-01-10 15:48:33

标签: php android mysql max min

这是 work_details表。现在我想从MySQL检索 timeIn timeOut 数据到android。

enter image description here

 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$000(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_WorkDetails$1GetHours.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_WorkDetails$1GetHours.onPostExecute(Edit_WorkDetails.java:220)
  

如何从最小ID中检索timeIn,从最大ID中检索timeOut   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();
        }
    }

被修改

enter image description here

假设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

它似乎只检索一行数据......

1 个答案:

答案 0 :(得分:1)

  

类型org.json.JSONArray无法转换为JSONObject

表示响应JSON字符串包含JSONObject 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); 。所以将JSON String转换为JSONArray然后迭代以从中获取所有JSONObjects,如:

{{1}}
相关问题