简体   繁体   English

针对一个PHP变量的多个MYSQL查询

[英]Multiple MYSQL Queries against one PHP variable

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. 我已经分解了单个SQL查询以获取我想要的来自SQL表的信息,但是我对如何在一个变量之间组合所有语句感到困惑。 This variable is being used in PHP to display my data. PHP中正在使用此变量来显示我的数据。

Here is the SQL queries I'm wanting to run. 这是我要运行的SQL查询。

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. 这是我当前的查询,它为我提供了我想要的一切,除了最近24小时的最大风速,最高温度和最低温度。

 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. 基本上,我只是想添加当前日期内出现的最高温度,最低温度和最大风速。

MySQL data table example MySQL数据表示例

Here is the way I'm trying to display the data using PHP. 这是我尝试使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM