sql - SQL 嵌套和/或语句
问题描述
我被困在一条 SQL 语句上。我正在尝试获得以下信息:
Select fields from the rentedHouseTbl WHERE
一个属性匹配一个属性IDTr
**AND**
the rentedHouseMoveInDateTr is less than or equal to today's date AND
the rentedHouseMoveOutDateTr is NULL
OR
the rentedHouseMoveInDateTr is less than or equal to today's date AND
the rentedHouseMoveOutDateTr is greater than today's date.
If this returns results then the property is occupied
else it is vacant
我使用的 SQL 语句是:
SELECT * FROM rentedHouseTbl where propertyIDTr = '1' AND ( (rentedHouseMoveInDateTr <= CURDATE() AND rentedHouseMoveOutDateTr IS NULL) OR (rentedHouseMoveInDateTr <= CURDATE() AND rentedHouseMoveOutDateTr > CURDATE()) )
但我没有得到预期的结果。任何帮助,将不胜感激!
解决方案
请试试这个:
SELECT * FROM rentedHouseTbl
WHERE propertyIDTr = '1'
AND rentedHouseMoveInDateTr <= CURDATE()
AND ISNULL(rentedHouseMoveOutDateTr, DATE_ADD(CURDATE(), INTERVAL -1 DAY)) <> CURDATE()
推荐阅读
- android - 从谷歌驱动器读取word文件时获取特殊字符
- python-2.7 - UnboundLocalError:在分配之前引用的局部变量没有任何作用,包括全局分配
- c# - 在 Windows 中捕获和阻止打印请求
- python - scipy.optimize.nnls 的多重解决方案
- excel - 对文本使用 IF 函数
- php - 页面加载时的 PHP 获取 UNIX 时间戳并保存在文本文件中
- regex - 使用 Regex (Rubular) 查找 URL 实例
- python - 通过关键字重命名熊猫中的多个列
- android - 导入 com.projectname.R 无法解析
- javascript - Gulp 任务以排除特定 js 文件写入 Index.html 文件