MySQL Left Join表,其中join表可以为null,但具有where条件

时间:2019-01-29 19:43:50

标签: mysql join mysqli

左连接表的最佳方法是什么?该表具有必须满足的特定条件,但同时也可能为空?

示例:

Select a.*, b.schedule
from Tablea a 
    left join Tableb b on a.subid = b.subid
WHERE 
    b.date > NOW()
    and b.active = 1

表B可能为空,但是如果表B不为空,则仅应显示活动的将来时间表。

3 个答案:

答案 0 :(得分:1)

您只需要将条件移至ON的{​​{1}}子句中即可:

JOIN

答案 1 :(得分:1)

您应在ON子句中添加左连接列的条件,否则将作为内部连接

    Select a.*, b.schedule
    from Tablea a 
    left join Tableb b on a.subid = b.subid AND  
        b.date > NOW()
        and b.active = 1

答案 2 :(得分:0)

只需测试一下就可以了,如果其他人有更好的解决方案,我会继续这样做,但这是我想出的

#Calculate Mean temperature and dew point
MeanTMP <- dframe %>% 
    mutate(MeanTMP=rowMeans(cbind(dframe$Max.Temp,dframe$Min.Temp),na.rm=TRUE))

#In my data, Minimum Dew point had more than 12 percent of missing data that is from 
# 2010 to 2018) so i want to exclude these years in mean but i dont know
# how do i do this?
MeanDTMP <- dframe %>% 
    mutate(MeanDTMP=rowMeans(cbind(dframe$Max.Dew.Point, dframe$Min.Dew.Point), 
           na.rm=TRUE))