I've broken down individual SQL queries for the information I want from my SQL table but I'm confused on how I can combine all statements agianst one variable. This variable is being used in PHP to display my data.
Here is the SQL queries I'm wanting to run.
SELECT * FROM weather ORDER BY stamp DESC LIMIT 1
SELECT SUM(rainfall) FROM weather WHERE stamp >= CURDATE()) AS total_rainfall
SELECT MAX(maxwind) FROM weather WHERE stamp >= CURDATE()) AS max_windspeed
SELECT MAX(temperature) FROM weather WHERE stamp >= CURDATE()) AS max_temperature
SELECT MIN(temperature) FROM weather WHERE stamp >= CURDATE()) AS min_temperature
Here is my current query which gives me everything I want except the max windspeed, max temperature, and minimum temperature for the last 24 hours.
SELECT *, (SELECT SUM(rainfall) FROM weather WHERE stamp >= CURDATE()) AS total_rainfall FROM weather ORDER BY stamp DESC LIMIT 1
Basically, I'm just wanting to add the maximum temperature, minimum temperature and maximum windspeed that occured within the current date.
Here is the way I'm trying to display the data using PHP.
<?php
$url1=$_SERVER['REQUEST_URI'];
header("Refresh: 60; URL=$url1");
$connectinfo = mysql_connect("***", "***", "***")
or die(mysql_error());
mysql_select_db("raspberrydb001", $connectinfo);
$sql = "SELECT *, (SELECT SUM(rainfall) FROM weatherdata WHERE stamp >= CURDATE()) AS total_rainfall FROM weatherdata ORDER BY stamp DESC LIMIT 1; ";
$result = mysql_query($sql, $connectinfo);
while($row = mysql_fetch_array($result)) {
$windspeed = $row['windspeed'];
$maxwind = $row['maxwind'];
$temperature = $row['temperature'];
$humidity = $row['humidity'];
$rainfall = $row['rainfall'];
$stamp = $row['stamp'];
$d=mktime();
$total_rainfall = $row['total_rainfall'];
echo "<div style='text-align:center'><h5>Temperature: " . $temperature . "(F)" . "<br>" . "Rainfall: " . $total_rainfall . "(in)" . "<br>" . "Wind: " . $windspeed . "(MPH)" . "<br>" . "Humidity: " . $humidity . "(%)" . "<br>" . "</h5></div>";
echo "<div style='text-align:right'><h6>Updated at: " . $stamp . "</h6></div>";
echo "<br>";
}
?>
Thanks
I would propose to split it up into two queries.
First, to collect all your data:
SELECT * FROM weather ORDER BY stamp DESC LIMIT 1
Second, to collect the min/max data:
SELECT
SUM(rainfall) AS total_rainfall,
MAX(maxwind) AS max_windspeed,
MAX(temperature) AS max_temperature,
MIN(temperature) AS min_temperature
FROM
weather
WHERE stamp >= CURDATE())
LIMIT 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.