MySQL中的查询/子查询问题

时间:2016-11-02 13:54:42

标签: mysql sql

我在查询(MySQL)中创建子查询时遇到了困难。

表名:log2016

数据记录于10月开始,表格如下:

| Date                | Temp | Hum | Cond          |
| 2016-10-01 14:03:40 | 13.6 | 60  | cloudy        |
| 2016-10-15 00:07:37 | 10.1 | 81  | partly cloudy |
   ...
| 2016-11-02 13:47:22 | 12.8 | 74  | partly cloudy |
  ...

首先,我想从2016-10-01 00:00:00中选择所有数据,直至2016-11-01 00:00:00

WHERE子句:

WHERE `Date` < '2016-11-01 00:00:00'

其次我想选择行&#34; Cond&#34;如下:

$query = "SELECT `Cond`, COUNT(`Cond`) AS `Anz` FROM `log2016` GROUP BY `Cond`"; 

这个查询工作正常,但是如果我把WHERE子句放在这个查询中,比如像。

$query = "SELECT `Cond`, COUNT(`Cond`) AS `Anz` FROM `log2016` WHERE `Date` < '2016-11-01 00:00:00' GROUP BY `Cond`";

它不会工作。

我搜索了很多内容并尝试了不同的方法来解决我的问题,但没有任何效果。

修改

以下是整个代码:

<?php

include_once("../../mysql/Wetterdatenlogger.php"); 
include_once("Wetter_2016.php");

    if (isset ($_GET['Periode']) && $_GET['Periode'] == 'ALL ') 
        {        
            $query = "SELECT `Wetter`, COUNT(`Wetter`) AS `Anzahl` FROM `log2016` GROUP BY `Wetter`";                
            $result = mysqli_query($db_link_Wetter, $query);

            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
                {
                    $Wetter1 = $row['Wetter'];
                    $Wetter = "'".$Wetter1."'";
                    $Anz = $row['Anzahl'];

                    $data[] = "[$Wetter, $Anz]";
                }
        }

    if (isset ($_GET['Periode']) && $_GET['Periode'] == 'OKT.') 
        {      
            $query = "SELECT `Wetter`, COUNT(*) AS `Anzahl` FROM 
                          (
                            SELECT * FROM `log2016` WHERE `Datum/Zeit` < '2016-11-01 00:00:00'
                          ) 
                        GROUP BY `Wetter`" ;

            $result = mysqli_query($db_link_RPI_1, $query);

            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
                {
                    $Wetter1 = $row['Wetter'];
                    $Wetter = "'".$Wetter1."'";
                    $Anz = $row['Anzahl'];

                    $data[] = "[$Wetter, $Anz]";
                }
        }
?>

JavaScript的:

<script type="text/javascript">
    $(function () {
        $('#container8').highcharts({
            chart: {
                type: 'pie',
                options3d: {
                    enabled: true,
                    alpha: 45,
                    beta: 0
                }
            },
            title: {
                text: 'Wetter 2016'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.2f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    depth: 35,
                    dataLabels: {
                        enabled: true,
                        format: '{point.name}'
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Wetter 2016',
                data: [<?php echo join($data, ',') ?>]
            }]
        });
    });
    </script>

HTML:

<form method="GET" id="choice" class="Wetter" action="Wetter_get_data_2016.php">
            <br>
            <input type="submit" name="Periode" value="OKT." class="input_Wetter">
            <input type="submit" name="Periode" value="NOV." class="input_Wetter">
            <input type="submit" name="Periode" value="DEZ." class="input_Wetter">
            <input type="submit" name="Periode" value="ALL " class="input_Wetter">
        </form>

        <script src="https://code.highcharts.com/highcharts.js"></script>
        <script src="https://code.highcharts.com/highcharts-3d.js"></script>
        <script src="https://code.highcharts.com/modules/exporting.js"></script>

        <div id="container8" style="height: 400px"></div>

输出&#34; ALL&#34;:

chart

输出&#34; OKT&#34; - 没有显示任何内容

2 个答案:

答案 0 :(得分:1)

我建议使用日期过滤进行内部选择,使用分组进行外部选择,例如:

SELECT Cond, COUNT(*) AS Anz FROM 
  (
    SELECT * FROM log2016 WHERE Date < '2016-11-01 00:00:00'
  ) 
GROUP BY Cond ;

答案 1 :(得分:0)

(代表OP发布)

问题解决了 - 有一个&#34;副本&amp;粘贴&#34;我的代码中出错。

查询现在有效。

感谢所有帮助者!