SELECT `location`.`building`, `location`.`classroom` FROM `schools`
LEFT OUTER JOIN `location`
ON `location`.`location_serial` = `classroom`.`location_serial`
LEFT OUTER JOIN `rack_info`
ON `classroom_info`.`location_serial` = `classroom`.`location_serial`
WHERE (
    (((`location`.`building` LIKE 'SCH%')))
    AND
    (`classroom_info`.`status` NOT IN (1, 2))
    AND
    (
        (
            (`location`.`building` = 'SCH1' AND `location`.`classroom` != '99')
            AND
            (`location`.`building` = 'SCH1' AND `location`.`classroom` != '34')
        )
    )
)


我的WHERE子句有问题。

假设location.building可以是SCH1,SCH2,SCH3,SCH4,CAB1,CAB2。我正在尝试选择所有SCH教室,其中一些教室状态为1和2。

这一切都很好

当我尝试进一步过滤结果以排除建筑物为SCH1且教室ID为99,建筑物为SCH1且教室ID为34的实例时,就会出现问题。我想显示所有SCH建筑物中的所有其他教室(包括SCH1,例如SCH1.98),但不包括上面的SQL查询中提到的两个特定教室(SCH1.99和SCH1.34)

我可能还想排除其他SCH房间,例如SCH2.24,SCH3.33-但我希望所有其他教室都出现。

我一直在找几个小时,我想我已经达到了我的能力极限-欢迎大家的帮助。

最佳答案

条件:


  排除建筑物为SCH1并且教室ID为99的实例


可以写成:

NOT (`location`.`building` = 'SCH1' AND `location`.`classroom` = '99')


要么:

(`location`.`building` <> 'SCH1' OR `location`.`classroom` <> '99')


这样做:

SELECT `location`.`building`, `location`.`classroom` FROM `schools`
LEFT OUTER JOIN `location`
ON `location`.`location_serial` = `classroom`.`location_serial`
LEFT OUTER JOIN `rack_info`
ON `classroom_info`.`location_serial` = `classroom`.`location_serial`
WHERE
    `location`.`building` LIKE 'SCH%'
    AND
    `classroom_info`.`status` NOT IN (1, 2)
    AND
    (`location`.`building` <> 'SCH1' OR `location`.`classroom` <> '99')
    AND
    (`location`.`building` <> 'SCH1' OR `location`.`classroom` <> '34')

07-26 04:33