Home > other >  Use multiple MAX() and MIN() in a query, or am I using >= NOW() - INTERVAL 1 DAY wrong?
Use multiple MAX() and MIN() in a query, or am I using >= NOW() - INTERVAL 1 DAY wrong?

Time:02-10

Can I use multiple MAX() and MIN() in a query? Or am I doing something else wrong in my code? I get the wrong results...

The code below gives me for t1 MAX 9.63 and MIN 10.00 in temperature.

9.63 is found at 2022-02-09 15:21:29 10 min before, it was 9.06, and 10 after it was 10.38 So, 9.63 is not a MAX nor a MIN number

10.0 is found at 2022-02-09 12:40:09 with higher and lower temps over and under.

//-----Get Max and Min------

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT 
MIN(t1) AS t1min, MAX(t1) AS t1max , 
MIN(t2) AS t2min, MAX(t2) AS t2max , 
MIN(t3) AS t3min, MAX(t3) AS t3max , 
MIN(t4) AS t4min, MAX(t4) AS t4max , 
MIN(t5) AS t5min, MAX(t5) AS t5max  
FROM ".$idpass." WHERE logdate >= NOW() - INTERVAL 1 DAY";  //Where i get $idpass = ($idpass <> "") ? $idpass : "Buapas"; 
//Tried CURDATE() - INTERVAL 1 DAY";
//Tried NOW() - INTERVAL 1 DAY";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc())    {

   $lastlogdate= $row['logdate'] ;
   $T1min= $row['t1min'] ;
   $T2min= $row['t2min'] ;
   $T3min= $row['t3min'] ;
   $T4min= $row['t4min'] ;
   $T5min= $row['t5min'] ;
   $T1max= $row['t1max'] ;
   $T2max= $row['t2max'] ;
   $T3max= $row['t3max'] ;
   $T4max= $row['t4max'] ;
   $T5max= $row['t5max'] ;
                                           }
                           } else {
   echo "0 results hentet fra databasen";
                                   }
$conn->close(); 

If i remove the

, MAX(t1) AS t1max , 
MIN(t2) AS t2min, MAX(t2) AS t2max , 
MIN(t3) AS t3min, MAX(t3) AS t3max , 
MIN(t4) AS t4min, MAX(t4) AS t4max , 
MIN(t5) AS t5min, MAX(t5) AS t5max  

from my code, it still says 10.0 in the MIN t1

CodePudding user response:

  •  Tags:  
  • Related