簡體   English   中英

在MySQL PHP中獲取最小ID和最大ID值

[英]Get min ID and max ID value in MySQL php

我想知道如何從MySQL php到android獲取具有最小id的timeIn和具有最大id的timeOut嗎?

這是表work_Details(id,project,percentage,timeIn,timeOut,twd)。 現在我想檢索timeIn : 12:26:00 and timeOut 11:26:00

在此處輸入圖片說明

  public void RetrieveTotalHours( final String ID)  // Assume ID is 69
    {
        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 {
            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);
            //total.setText(MiNtimeIn);

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

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);

?>

輸出錯誤

01-12 12:43:41.540  22692-22692/com.example.project.myapplication E/A﹕ 20:26:00
01-12 12:43:41.540  22692-22692/com.example.project.myapplication E/S﹕ 11:26:00

它檢索具有最大id的timeIn和timeOut ...

檢索具有最大id的timeIn和timeOut ...

這樣做才能得到MiNtimeIn從第一的JSONObject和MaXtimeOut從第二的JSONObject:

String MiNtimeIn,MaXtimeOut;
JSONArray array=new JSONArray(json);
if(array.length()<2){
  JSONObject jsonObject = array.getJSONObject(0);
  MiNtimeIn = jsonObject.optString(Configs.TAG_IN);
  MaXtimeOut=jsonObject.optString(Configs.TAG_OUT);
}else{
  // get First Object from JSONArray
   JSONObject oneObject = array.getJSONObject(0);
  MiNtimeIn = oneObject.optString(Configs.TAG_IN); // get min from first row
  // get Second Object from JSONArray
   JSONObject twoObject = array.getJSONObject(array.length()-1);
   MaXtimeOut = twoObject.optString(Configs.TAG_OUT); // get min from second row
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM