2011-03-18 101 views
1
SELECT * FROM event JOIN user ON event.event_user_id=user.user_id 
    WHERE ((event.event_date BETWEEN '2011-03-01' AND '2011-04-01') 
    AND user.user_id in (SELECT user_id FROM user_map 
     WHERE (user_map.user_id_map=1 AND user_map.user_relation<=7))); 

蟾蜍/ MySQL的這是工作正常,但失敗時,我在Java + MySQL驅動程序準備好的聲明中做得一樣哪些錯誤與此MySQL查詢

SELECT * FROM event JOIN user ON event.event_user_id=user.user_id 
    WHERE ((event.event_date BETWEEN ? AND ?) 
    AND user.user_id in (SELECT user_id FROM user_map 
     WHERE (user_map.user_id_map=? AND user_map.user_relation<=?))); 

...

pstmt.setDate(1, <sqlDate>); 
pstmt.setDate(2, <sqlDate>); 
pstmt.setLong(3, <int>); 
pstmt.setLong(4, <int>); 

pstmt.executeQuery(); 
+4

指標「失敗」非常含糊......謹慎告訴我們究竟發生了什麼?它執行但返回錯誤的結果嗎?提供錯誤訊息?如果是後者,那麼錯誤信息是什麼? – 2011-03-18 06:27:35

+1

顯示堆棧跟蹤。 – Nishant 2011-03-18 06:33:14

+0

當你最後的代碼片段發佈時,它也會很有用。有些錯誤可以通過將參數傳遞給準備陳述方法來完成,我們無法通過依次檢查實際參數等等。 – Chris 2011-03-18 08:23:26

回答

1

它不是密切相關的問題,但查詢效率不高,可以提高

SELECT * 
FROM event 
     JOIN USER 
     ON event.event_user_id = USER.user_id 
     JOIN user_map 
     ON user_map.user_id = USER.user_id 
      AND user_map.user_id_map = 1 
      AND user_map.user_relation<=7 
WHERE (event.event_date BETWEEN '2011-03-01' AND '2011-04-01') 
GROUP BY USER.user_id 

你需要

  • (event.event_user_id,event.event_date)
  • (USER.user_id)
  • (user_map.user_id,user_map.user_id_map,user_map.user_relation)
+0

謝謝!我錯過了最後一行中「user_map」和「WHERE」之間的空格。這只是我在發佈之前的空間問題。但我很高興我做到了。你querry似乎很有效,也更容易閱讀。每天學習!再次感謝 – 2011-03-20 04:35:58