并非左边的所有字符串都加入

时间:2017-02-13 06:48:30

标签: mysql

我有这个查询

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中,有些字符串的类型不在第二个表中。但是根据要求不会显示它们。

2 个答案:

答案 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'