0

我有一个名为 eleva 的表,其中包含 inicio_periodo、id 列,当满足条件时,我想用来自该表的一些数据填充另一个表。我正在尝试应该工作的 where 语句,但无论条件如何,它都会插入到另一个表中,这是我的代码:

for($i=0; $i < $tamanho_array_eleva-1; $i++) {
    if($array_eleva[$i+1] - $array_eleva[$i] > 1){
        $con->query("
                    INSERT INTO eventos_dias
                    SELECT DATE(inicio_periodo),'1','0','0','0'
                    FROM eleva
                    WHERE id = $array_eleva[$i] AND 0<=HOUR(inicio_periodo)<6");
        $con->query("
                    INSERT INTO eventos_dias
                    SELECT DATE(inicio_periodo),'0','1','0','0'
                    FROM eleva
                    WHERE id = $array_eleva[$i] AND 6<=HOUR(inicio_periodo)<12");
        $con->query("
                    INSERT INTO eventos_dias
                    SELECT DATE(inicio_periodo),'0','0','1','0'
                    FROM eleva
                    WHERE id = $array_eleva[$i] AND 12<=HOUR(inicio_periodo)<18");
        $con->query("
                    INSERT INTO eventos_dias
                    SELECT DATE(inicio_periodo),'0','0','0','1'
                    FROM eleva
                    WHERE id = $array_eleva[$i] AND 18<=HOUR(inicio_periodo)<24");
    }
}

我知道它不起作用,因为目前在表上我只有 16 小时的数据......但无论如何它都在执行所有插入。有人看到错误吗?

只要有帮助:我的想法是在number<=hour(inicio_periodo)<number满足条件时用所选值填充另一个表。提前致谢!

4

1 回答 1

1

此查询向您展示了原因:

mysql> SELECT 1<= 16 < 6;
+------------+
| 1<= 16 < 6 |
+------------+
|          1 |
+------------+

就像它一样

mysql> SELECT (1 <= 16) < 6;
+---------------+
| (1 <= 16) < 6 |
+---------------+
|             1 |
+---------------+

变成:

mysql> SELECT 1 < 6;
+-------+
| 1 < 6 |
+-------+
|     1 |
+-------+

同样:18 <= 16 < 24=> (18 <= 16) < 24=> 0 < 24=>1

简而言之,使用value BETWEEN min AND max,这就是它的用途,或者使用单独的检查:

mysql> SELECT 16 BETWEEN 1 AND 6;
+--------------------+
| 16 BETWEEN 1 AND 6 |
+--------------------+
|                  0 |
+--------------------+

 mysql> SELECT 1 <= 16 AND 16 < 6;
+--------------------+
| 1 <= 16 AND 16 < 6 |
+--------------------+
|                  0 |
+--------------------+
1 row in set (0.00 sec) 
于 2013-10-17T23:12:55.083 回答