【发布时间】:2011-03-13 20:04:23
【问题描述】:
如果这是一个新手问题,我很抱歉,但我似乎不明白为什么这不像我想要的那样工作:
mysql> select t.id,t.date_fin_val,tc.date_fin_val
from tiers t
join tiers_critere_int tc on tc.id_tiers=t.id
where (t.date_fin_val is null) and (tc.date_fin_val is null);
+----+---------------------+---------------------+
| id | date_fin_val | date_fin_val |
+----+---------------------+---------------------+
| 1 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
| 1 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
| 1 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+----+---------------------+---------------------+
3 rows in set (0.00 sec)
mysql> select t.id,t.date_fin_val,tc.date_fin_val
from tiers t
left outer join tiers_critere_int tc on tc.id_tiers=t.id
where (t.date_fin_val is null) and (tc.date_fin_val is null);
Empty set (0.00 sec)
mysql>
我认为“左外连接”的意思是:“如果右侧没有结果,但左侧有一个,则继续无论如何将左侧的那个放在“@ 987654322@" 右侧的值。
如果我是对的,使用“left outer join”而不是“join”的第二个查询应该返回值。但事实并非如此。为什么?
这是我的数据:
mysql> select * from tiers t where date_fin_val is null;
+----+---------------------+--------------------+
| id | date_fin_val | est_tiers_physique |
+----+---------------------+--------------------+
| 1 | 0000-00-00 00:00:00 | 1 |
+----+---------------------+--------------------+
1 row in set (0.00 sec)
mysql> select * from tiers_critere_int where date_fin_val is null;
+----+---------------------+----------+------------+---------+
| id | date_fin_val | id_tiers | id_critere | critere |
+----+---------------------+----------+------------+---------+
| 1 | 0000-00-00 00:00:00 | 1 | 2 | 86 |
| 2 | 0000-00-00 00:00:00 | 1 | 6 | 170 |
| 3 | 0000-00-00 00:00:00 | 1 | 7 | 65 |
+----+---------------------+----------+------------+---------+
3 rows in set (0.00 sec)
mysql>
非常感谢!
【问题讨论】:
-
你的意思是说 second 查询不返回值吗?
-
我对第一个查询如何返回结果感到困惑。在 where 子句中,您指示两个日期值都必须为空,但在结果集中,两个日期值都不是空的。无论是联接还是左联接,查询都应返回相同的结果。左联接不应得到更少的结果,只有当右表中的相应字段为空时才会得到更多结果。
-
向我们展示在 tiers 和 tiers_critre_int(id 为 1)表中存在哪些类型的数据。
-
@Tandu 如果
datetime是null,那么MySQL 返回"0000-00-00 00:00:00"。因此,您看到的结果。无论如何,我已经在我的问题中添加了表格中的数据。 -
@OlivierDofus 据我所知这是不正确的,它将返回 null。我自己运行了查询,但没有得到任何结果。