2016-01-25 177 views
0

我想根據from_date是今天或以後的條件查詢搜索結果,並且to_date如果存在於該範圍內,則直到今天。根據日期查詢數據 - Mysql

我的查詢如下:

SELECT * 
FROM device b LEFT OUTER JOIN device_model a 
    ON (b.`model_ID`=a.`model_ID`) 
LEFT JOIN sub_product_area c 
    ON (b.sub_Product_Area_ID = c.sub_Product_Area_ID) 
LEFT JOIN borrow_device d 
    ON (b.device_ID = d.device_ID AND (from_Date >= CURDATE() or CURDATE() <= to_date)) 
WHERE (a.`name`LIKE '%galaxy%') OR (a.platform LIKE 'galaxy%') OR 
    (a.`type`LIKE '%galaxy%') OR (a.`OS`LIKE '%galaxy%') OR (d.username LIKE '%galaxy%') OR 
    (c.`sub_Product_Area_name` LIKE '%galaxy%') 
ORDER BY d.`transaction_Mode` DESC 

我的表結構在這裏:

enter image description here

問題出在哪裏後「或」下一項得到它不顯示結果真正。我的查詢有什麼問題嗎?

+0

你的約會條件對我沒有意義。舉個例子,你可以通過'from_date'和'to_date'來證明你想保留哪些類型的記錄? –

+0

我想顯示from_date> = today或to_date> = today的結果。這是from_date是今天或更新日期或to_date是今天或未來日期的地方。希望這已經足夠清楚 – Nez

+0

爲什麼from_date永遠爲空?並考慮提供適當的CREATE和INSERT語句以及所需的結果。 – Strawberry

回答

0

你把你的條件加入子句中,你應該把它弄出來是:

SELECT * 
FROM device b LEFT OUTER JOIN device_model a 
    ON (b.`model_ID`=a.`model_ID`) 
LEFT JOIN sub_product_area c 
    ON (b.sub_Product_Area_ID = c.sub_Product_Area_ID) 
LEFT JOIN borrow_device d 
    ON b.device_ID = d.device_ID 
WHERE (from_Date >= CURDATE() or CURDATE() <= to_date) AND ((a.`name`LIKE '%galaxy%') OR (a.platform LIKE 'galaxy%') OR 
    (a.`type`LIKE '%galaxy%') OR (a.`OS`LIKE '%galaxy%') OR (d.username LIKE '%galaxy%') OR 
    (c.`sub_Product_Area_name` LIKE '%galaxy%')) 
ORDER BY d.`transaction_Mode` DESC 
0

極品正確的這一個,並嘗試:

LEFT JOIN borrow_device d 
    ON (b.device_ID = d.device_ID AND (from_Date >= CURDATE() or CURDATE() <= to_date)) 

TO:

LEFT JOIN borrow_device d 
    ON (b.device_ID = d.device_ID AND (from_Date >= CURDATE() or to_date >= CURDATE())) 
0

1)我看過了(a.platform LIKE'galaxy%')「,而不是」(a.platform LIKE'**%** galaxy%')「。如果'to_date'可能爲空,嘗試添加一個條件來檢查連接條件爲空(即to_date爲空或。)。如果'to_date'可能爲空,則嘗試添加條件以檢查連接條件中的空值(或to_date爲空或。 ..)

3)考慮用字符「`」包裝字段名稱。

4)你有沒有試過你的查詢沒有where子句?

希望它適合你。