2011-12-15 37 views
-1

這是我的mysql查詢。我想要提取的是;所有在過去6天內插入的行,包括今天。這裏是我的查詢mySql中的多個BETWEEN&multiple AND查詢給出錯誤的答案

$user_ids = $_SESSION['user_ids']; //its array 
    $cs_user_ids =implode(",",$user_ids); 


    SELECT fb_user.name,schedule.id,schedule.location,schedule.arrival, 
    schedule.departure,schedule.notes,schedule.lat, 
    schedule.lon,schedule.who_can_c_me 
    FROM schedule,fb_user 
    WHERE EXTRACT(DAY FROM arrival) 
    BETWEEN EXTRACT(DAY FROM DATE_SUB(CURDATE(),INTERVAL 6 DAY)) 
    AND  EXTRACT(DAY FROM CURDATE()) 
    AND  EXTRACT(DAY FROM departure) 
    BETWEEN EXTRACT(DAY FROM DATE_SUB(CURDATE(),INTERVAL 6 DAY)) 
    AND  EXTRACT(DAY FROM CURDATE()) 
    AND  fb_user.fb_id = schedule.fb_id 
    AND fb_user.fb_id IN($cs_user_ids) 

我在這個statment想OR這裏代替AND EXTRACT(DAY FROM departure)。因爲我需要抵達出發日期但它不能與OR它與AND一起工作,我不需要。什麼是我的查詢錯誤?我想用()

回答

-2

嘗試這種方式使用括號支架

"SELECT fb_user.name,schedule.id,schedule.location,schedule.arrival,schedule.departure,schedule.notes,schedule.lat,schedule.lon,schedule.who_can_c_me 
    FROM schedule,fb_user 
    WHERE EXTRACT(DAY FROM arrival) 
    (BETWEEN EXTRACT(DAY FROM DATE_SUB(CURDATE(),INTERVAL 6 DAY)) AND EXTRACT(DAY FROM CURDATE())) 

    OR  
    EXTRACT(DAY FROM departure) 
    (BETWEEN EXTRACT(DAY FROM DATE_SUB(CURDATE(),INTERVAL 6 DAY)) AND EXTRACT(DAY FROM CURDATE())) 

    AND fb_user.fb_id = schedule.fb_id 
    AND fb_user.fb_id IN($cs_user_ids)" 
+1

我不認爲這符合這個:***到​​達或離開日期***。可以? –

+0

這是很好的接受答案與負面投票:-) – Pratik

+1

woow現在在答案中有什麼錯誤,請建議至少如果你給-1所以糾正它。 – Pratik

0

問題,這應該工作:

SELECT 
     fb_user.name,schedule.id,schedule.location,schedule.arrival,schedule.departure, 
     schedule.notes,schedule.lat,schedule.lon,schedule.who_can_c_me 
FROM 
     schedule,fb_user 
WHERE  
    (
    (EXTRACT(DAY FROM arrival) BETWEEN EXTRACT(DAY FROM DATE_SUB(CURDATE(),INTERVAL 6 DAY)) AND EXTRACT(DAY FROM CURDATE())) 
    OR 
    (EXTRACT(DAY FROM departure) BETWEEN EXTRACT(DAY FROM DATE_SUB(CURDATE(),INTERVAL 6 DAY)) AND EXTRACT(DAY FROM CURDATE())) 
    ) 
    AND  
     fb_user.fb_id = schedule.fb_id 
    AND 
     fb_user.fb_id IN($cs_user_ids) 
+0

讓我試試這個.. –

0

試試這個:

SELECT fb_user.name,schedule.id,schedule.location,schedule.arrival,schedule.departure,schedule.notes,schedule.lat,schedule.lon,schedule.who_can_c_me 
    FROM schedule,fb_user 
    WHERE ((DATE(arrival) BETWEEN DATE_SUB(CURDATE(),INTERVAL 6 DAY) AND CURDATE()) 
    OR (DATE(departure) BETWEEN DATE_SUB(CURDATE(),INTERVAL 6 DAY) AND CURDATE())) 
    AND fb_user.fb_id = schedule.fb_id AND fb_user.fb_id IN($cs_user_ids) 

我覺得用提取DAY的會像第1或第2天那樣引起意想不到的結果,並且這種比較...避免它。