mysql
2014-01-25 360 views 2 likes 
2

計數傳真由夜班晚上10點完成的 - 早上7點,MYSQL日期範圍和時間範圍

日期和時間,在不同的領域

$stmt = $db -> prepare("SELECT count(*) FROM fax WHERE 
       date BETWEEN CURDATE() and CURDATE() + INTERVAL 1 DAY 
        and time >= '22:00:00' and time <= '7:00:00' 
         and shift='GY' 
          and complete=1");     
    $stmt -> execute(); 
    echo $GY_COMP = $stmt -> fetchColumn(); 

這個查詢總是產生0,但有數據。

有人可以幫我查詢嗎? 感謝提前:)

+0

你從db中獲得計數的方式是錯誤的。你應該選擇計數,而不是記錄來計算它們。 –

+0

謝謝你。這是一個壞習慣。你可以檢查我的答案下面我在這個聲明中有另一個問題 – Newbie

回答

4

我認爲這個問題是你的時間比較:您有效地過濾掉一切與你的time >= '22:00:00' and time <= '7:00:00'

希望這有助於:http://sqlfiddle.com/#!2/45108/7/0

SELECT * FROM fax 
WHERE date BETWEEN CURDATE() and CURDATE() + INTERVAL 1 DAY 
and 
((time >= '22:00' and time <= '23:59') 
or 
(time >= '0:00' and time <= '7:00')) 
and shift='GY' 
and complete=1 
+0

哦,非常感謝最大 – Newbie

+0

最大可以檢查我的答案我需要幫助在IF ELSE MYSQL。謝謝 – Newbie

0

感謝爲最大時間比較, 我已經對語法做了更改

SELECT count(*) FROM fax 
    WHERE ((date = CURDATE() and (time >='22:00' and time <= '23:59')) 
      or (date=CURDATE() + INTERVAL 1 DAY and (time >= '00:00' and time <= '7:00'))) 
        and shift = 'GY' 
          and complete = 1 

現在我的問題是移位的範圍是今天晚上10點到湯姆7am。

但是,當更改日期

HELP在使用我的語法上面仍然得到錯誤的if語句MYSQL

IF YES(選擇此)ELSE IF(選擇此) 如何正確IF ELSE在使用MYSQL語句

SELECT count(*) FROM fax 
    IF 
    (date = CURDATE() and (time >='22:00' and time <= '23:59') 
    THEN 
     SELECT count(*) FROM fax 
     WHERE ((date = CURDATE() and (time >='22:00' and time <= '23:59')) 
     or (date=CURDATE() + INTERVAL 1 DAY and (time >= '00:00' and time <= '7:00'))) 
     and shift = 'GY' and complete = 1 
    ELSE IF 
    (date = CURDATE() and (time >= '00:00' and time <= '7:00') 
      THEN 
      SELECT count(*) FROM fax 
      WHERE ((date = CURDATE() and (time >= '00:00' and time <= '7:00')) 
      or (date=CURDATE() - INTERVAL 1 DAY and (time >='22:00' and time <= '23:59'))) 
      and shift = 'GY' and complete = 1 
+0

請將您添加到最初的問題中:不要刪除原始文本,而是在「更新」文本後附加新文本。另外,你最好描述一下你的目標是什麼:例如,「我保持......在一個帶有字段的表格中......我想獲得所有的......例如,對於這個樣本記錄...我想得到這個輸出......「 –

相關問題