MySQL:如何使用某些条件连接2表

时间:2018-07-03 09:11:12

标签: mysql sql

目前我无法在有条件的情况下加入2张桌子, 下面是我的sql:

select x.promo_start_date  from `ms_promotion_reward` promos LEFT JOIN `ms_promotion` x ON `x`.`PromotionUUID` = `promos`.`PromotionUUID` and `x`.`promo_start_date <= NOW()` and `x`.`promo_end_date >= NOW()` 

如果今天的日期在promo_start_date和promo_end_date之间,我想加入表格。

但是即使promo_start_date字段位于ms_promotion中,我总是会收到错误消息

#1054 - Unknown column 'x.promo_start_date <= NOW()' in 'on clause'

那么该问题的解决方案是什么?

2 个答案:

答案 0 :(得分:1)

您在查询中放错了引号。试试这个。

select x.promo_start_date  from `ms_promotion_reward` promos LEFT JOIN `ms_promotion` x ON `x`.`PromotionUUID` = `promos`.`PromotionUUID` and `x`.`promo_start_date` <= NOW() and `x`.`promo_end_date` >= NOW()

答案 1 :(得分:0)

您在promo_end_dat >= NOW()处的引号使mysql认为它是一个列名。将其替换为以下语句

select x.promo_start_date  from `ms_promotion_reward` promos LEFT JOIN `ms_promotion` x ON `x`.`PromotionUUID` = `promos`.`PromotionUUID` AND`x`.`promo_start_date` <= NOW() AND `x`.`promo_end_date` >= NOW()