2014-03-07 180 views
0

查询做些什么: 它获取所需的数据,例如事件标题,帖子的名字,星期,事件开始时间,事件结束时间根据当前时间即将举行的活动。MySQL的JOIN条件查询

我从PHP日期('w')和当前时间传递周日值。

问题:我希望它只匹配当前工作日的start_hour(本例中为5)。我在哪里有AND wp_wcs3_schedule.start_hour > '09:00:00'需要限制此情况适用于当前工作日,在这种情况下为“5”。

SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_name, wp_wcs3_schedule.weekday, 
    wp_wcs3_schedule.start_hour, wp_wcs3_schedule.end_hour 
FROM wp_wcs3_schedule 
INNER JOIN wp_posts 
WHERE wp_wcs3_schedule.class_id = wp_posts.ID 
AND wp_wcs3_schedule.weekday >=5 
AND wp_wcs3_schedule.start_hour > '09:00:00' 
AND wp_posts.post_title NOT LIKE '%squash%' 
AND wp_posts.post_title NOT LIKE '%sal%' 
ORDER BY wp_wcs3_schedule.weekday, wp_wcs3_schedule.start_hour ASC LIMIT 0,3 

回答

1

为此,您可以使用easliy OR,所以

AND (wp_wcs3_schedule.weekday > 5 OR wp_wcs3_schedule.start_hour > '09:00:00') 

你已经有> = 5所以.weekday将是5或以上,而这则仅适用起始时间标准的与工作日== 5.这给最后的查询

SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_name, wp_wcs3_schedule.weekday, 
    wp_wcs3_schedule.start_hour, wp_wcs3_schedule.end_hour 
FROM wp_wcs3_schedule 
INNER JOIN wp_posts 
WHERE wp_wcs3_schedule.class_id = wp_posts.ID 
AND wp_wcs3_schedule.weekday >=5 
AND (wp_wcs3_schedule.weekday > 5 OR wp_wcs3_schedule.start_hour > '09:00:00') 
AND wp_posts.post_title NOT LIKE '%squash%' 
AND wp_posts.post_title NOT LIKE '%sal%' 
ORDER BY wp_wcs3_schedule.weekday, wp_wcs3_schedule.start_hour ASC LIMIT 0,3 
+0

谢谢非常! – Jade

+0

我正在使用AND而不是OR逻辑,我猜想 – Jade

+1

在这里也很重要。没有他们会产生意想不到的结果。 –