我有这个查询
select delivery_first_week_repo.type, delivery_first_week_repo.from_sale, delivery_week_repo.from_sale
from delivery_first_week_repo
LEFT JOIN delivery_week_repo on delivery_first_week_repo.type = delivery_week_repo.type
where delivery_first_week_repo.week_day = 'Tuesday'
and delivery_week_repo.week_day = 'Tuesday'
在delivery_first_week_repo
中,有些字符串的类型不在第二个表中。但是根据要求不会显示它们。
答案 0 :(得分:1)
如果第二个表中没有匹配的行,则第二个表中的列将在连接中为空。因此delivery_week_repo.week_day = 'Tuesday'
对于那些行永远不会成立。
第二个表的条件应该放在ON
子句中,而不是WHERE
子句中。
select delivery_first_week_repo.type, delivery_first_week_repo.from_sale, delivery_week_repo.from_sale
from delivery_first_week_repo
LEFT JOIN delivery_week_repo on delivery_first_week_repo.type = delivery_week_repo.type
and delivery_week_repo.week_day = 'Tuesday'
where delivery_first_week_repo.week_day = 'Tuesday'
答案 1 :(得分:0)
这是因为 LEFT JOIN 类型。
使用内部联接将为您提供匹配结果
select delivery_first_week_repo.type,
delivery_first_week_repo.from_sale, delivery_week_repo.from_sale
from delivery_first_week_repo
INNER JOIN delivery_week_repo on delivery_first_week_repo.type =
delivery_week_repo.type
where delivery_first_week_repo.week_day = 'Tuesday'
and delivery_week_repo.week_day = 'Tuesday'