2014-03-07 101 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

在這裏也很重要。沒有他們會產生意想不到的結果。 –