首页 > 解决方案 > 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()) )

但我没有得到预期的结果。任何帮助,将不胜感激!

标签: sql

解决方案


请试试这个:

SELECT * FROM rentedHouseTbl 
  WHERE propertyIDTr = '1' 
     AND rentedHouseMoveInDateTr <= CURDATE() 
     AND ISNULL(rentedHouseMoveOutDateTr, DATE_ADD(CURDATE(), INTERVAL -1 DAY)) <> CURDATE()

推荐阅读