Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在mysql中是否可以使用多个查询?_Php_Mysql_Database_Date_Group By - Fatal编程技术网

Php 在mysql中是否可以使用多个查询?

Php 在mysql中是否可以使用多个查询?,php,mysql,database,date,group-by,Php,Mysql,Database,Date,Group By,嗨,伙计们,我有一个疑问,我有我的网站,我试图用php从我的数据库回显最高温度,最低温度,最近进入数据库的温度,我四处查看了一下,似乎我需要使用Mysql中的UNION使用2个查询,我使用了它,但现在它只显示了当天的最高温度(以DB为单位) 这是我的密码: $connect = mysqli_connect(".....", ".....", "....", "....."); $sql ="SELECT MAX(temperature) as max_temperature , MIN(tem

嗨,伙计们,我有一个疑问,我有我的网站,我试图用php从我的数据库回显最高温度,最低温度,最近进入数据库的温度,我四处查看了一下,似乎我需要使用Mysql中的UNION使用2个查询,我使用了它,但现在它只显示了当天的最高温度(以DB为单位) 这是我的密码:

$connect = mysqli_connect(".....", ".....", "....", ".....");
$sql ="SELECT MAX(temperature) as max_temperature , MIN(temperature) as min_temperature 
        FROM sensor 
        WHERE DATE(data) = CURDATE() 
        UNION 
        SELECT temperature, data 
        FROM sensor 
        ORDER BY data DESC 
        LIMIT 1";

$result = mysqli_query($connect, $sql);

while($row = mysqli_fetch_array($result)) {
    $max_temperature = number_format($row['max_temperature'], 1, '.', ',');/*armazena dados vindo da B.D  */
    $temperature = number_format($row['temperature'], 1, '.', ',');
    $min_temperature = number_format($row['min_temperature'], 1, '.', ',');
}

echo "<h4>".$max_temperature."</h4>";
echo "<h3>".$temperature."</h3>";
echo "<h4>".$min_temperature."</h4>"; 
非常感谢所有的帮助

在这里,联合似乎是不必要的,它增加了客户端的复杂性。通过以下查询,您可以在一行上获得所需的三个信息,而无需联合:

SELECT 
    MAX(temperature) max_temperature, 
    MIN(temperature) min_temperature,
    (SELECT temperature FROM sensor ORDER BY data DESC LIMIT 1) current_temperature
FROM sensor
WHERE data >= current_date AND data < current_date + 1 day
我不确定窗口功能是否会表现得更好,但这需要MySQL 8.0:

SELECT DISTINCT
    MAX(temperature) OVER() max_temperature, 
    MIN(temperature) OVER() min_temperature,
    FIRST_VALUE(temperature) OVER(ORDER BY date DESC) current_temperature
FROM sensor
WHERE data >= current_date AND data < current_date + interval 1 day
在这里,联合似乎是不必要的,它增加了客户端的复杂性。通过以下查询,您可以在一行上获得所需的三个信息,而无需联合:

SELECT 
    MAX(temperature) max_temperature, 
    MIN(temperature) min_temperature,
    (SELECT temperature FROM sensor ORDER BY data DESC LIMIT 1) current_temperature
FROM sensor
WHERE data >= current_date AND data < current_date + 1 day
我不确定窗口功能是否会表现得更好,但这需要MySQL 8.0:

SELECT DISTINCT
    MAX(temperature) OVER() max_temperature, 
    MIN(temperature) OVER() min_temperature,
    FIRST_VALUE(temperature) OVER(ORDER BY date DESC) current_temperature
FROM sensor
WHERE data >= current_date AND data < current_date + interval 1 day