【问题标题】:Get min ID and max ID value in MySQL php在 MySQL php 中获取最小 ID 和最大 ID 值
【发布时间】:2016-04-16 14:26:24
【问题描述】:

我想知道如何获取具有最小 id 的 timeIn 和具有从MySQL php 到 android 的最大 id 的 timeOut

这是表格 work_Details(id,project,percentage,timeIn,timeOut,twd)。现在我想检索timeIn : 12:26:00timeOut 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...

【问题讨论】:

  • 您想实现哪个逻辑来检索timeIn : 12:26:00 and timeOut 11:26:00 值?因为你从 db 得到两行,你想使用第一行的 timeIn 和第二行的 timeOut 对吗?
  • @ρяσѕρєяK 正是..有可能实现吗?

标签: php android mysql json


【解决方案1】:

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

从第一个 JSONObject 中获取 MiNtimeIn,从第二个 JSONObject 中获取 MaXtimeOut

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
}

【讨论】:

  • 如果我不想修复array.length,我该怎么办?假设行数不固定
  • @JohnJoe:如果行数不固定,那么您需要开发一个逻辑来选择TAG_IN TAG_OUT ,目前在我的回答逻辑中是:如果找到单行,那么它将同时显示对于该行,否则显示第一行的 TAG_IN 和最后一行的 TAG_OUT。
  • 如果我有100行,你的答案还能用吗?
  • @JohnJoe:你告诉如果你有 100 行那么你想要什么?
  • 从第一行获取timeIn,从最后一行获取timeOut
猜你喜欢
  • 2016-04-14
  • 2016-04-14
  • 1970-01-01
  • 2021-03-02
  • 2021-06-02
  • 2019-02-12
  • 2011-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多