我有一个名为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
满足,我的想法是用所选值填充另一个表。提前谢谢!
答案 0 :(得分: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
=&gt; (18 <= 16) < 24
=&gt; 0 < 24
=&gt; 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)