根据日期查询数据 - Mysql

时间:2016-01-25 06:45:25

标签: mysql date

我希望显示基于from_date今天或更高的条件查询的搜索结果,并且如果该范围内存在,则to_date将一直持续到今天。

我的查询如下:

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

问题是,在&#39; OR&#39;之后的下一个子句中,它没有显示结果。得到了。我的查询有什么问题吗?

3 个答案:

答案 0 :(得分:0)

你把你的条件放在了join子句中,你应该把它弄清楚:

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

答案 1 :(得分:0)

需要更正此并尝试:

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

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

答案 2 :(得分:0)

1)我已经看到a.platform的条件为“(a.platform LIKE'galaxy%')”而不是“(a.platform LIKE'**%** galaxy%')”(即' %'对于'galaxy%'部分与where子句的其他部分一样。)

2)如果'to_date'可以为null,请尝试添加条件以检查连接条件中的空值,即(或to_date为空或......)

3)考虑用字符“`”包装字段名称。

4)你是否尝试过没有where子句的查询?

希望它适合你。