在MySQL php中获取最小ID和最大ID值

在MySQL php中获取最小ID和最大ID值,php,android,mysql,json,Php,Android,Mysql,Json,我想知道如何从MySQLphp到android获取具有最小id的timeIn和具有最大id的timeOut 这是表格工作的详细信息(id、项目、百分比、时间、超时、twd)。现在我想检索timeIn:12:26:00和timeOut 11:26:00 它检索具有最大id的timeIn和timeOut 检索具有最大id的timeIn和timeOut 从第一个JSONObject获取MiNtimeIn,从第二个JSONObject获取MaXtimeOut: String MiNtimeIn,MaX

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

这是表格工作的详细信息(id、项目、百分比、时间、超时、twd)。现在我想检索
timeIn:12:26:00
timeOut 11:26:00

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

检索具有最大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
}
String MiNtimeIn,MaXtimeOut;
JSONArray数组=新的JSONArray(json);

if(array.length()您想要实现哪个逻辑来检索
timeIn:12:26:00和timeOut 11:26:00
值?因为您从db中获取两行,并且您想要从第一行使用
timeIn
,从第二行使用
timeOut
,对吗?@ρσѕρєK确切地说..有可能实现吗?如果我不想修复数组的长度,应该怎么做我该怎么办?假设行数没有fixed@JohnJoe:如果行数不固定,那么您需要开发一个逻辑来选择
TAG_IN
TAG_OUT
,当前在我的回答中,逻辑是:如果找到一行,则该行将同时显示这两行,否则从第一行显示TAG_IN,从最后一行显示TAG_OUT。如果我有10行0行,你的答案还是可以用的对吗?@JohnJoe:如果你有100行,那么你想要什么?从第一行开始计时,从最后一行开始超时,先生
<?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
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
}