gpt4 book ai didi

mysql - WHERE 子句后跟 OR 然后再 WHERE

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:46 26 4
gpt4 key购买 nike

我将只发布包含 WHERE 子句的 sql statament 部分:

//Together
WHERE `energy_extra`.`PacketFk` IN('7')
OR `energy_extra`.`house_extra_id` IN('4')

//Not together (separate where statements)
AND `People_from` <= '4'
AND `People_to` >= '4'
AND `Size` >= '100'
AND `Size` <= '200'
AND `houseconstructiontypelist`.`ConstructionTypeFk` IN('1', '2')

我无法更改这些行的顺序,因为它们是用条件语句在 codeigniter 事件记录中编写的更大查询的一部分。我可以更改的是 SQL 本身。

我没有得到想要的结果,因为 WHERE foo IN ('7') OR bar IN ('4') 后跟 AND 因此 SQL 认为这些进一步的条件是 OR bar IN() 的一部分,但它们不是。他们需要以新的 WHERE 语句开始,之后的每一行都可以再次以 AND 开始。

我试过了,但没用:

//Together
WHERE `energy_extra`.`PacketFk` IN('7')
OR `energy_extra`.`house_extra_id` IN('4')

//Not together (separate where statements)
WHERE `People_from` <= '4'
AND `People_to` >= '4'
AND `Size` >= '100'
AND `Size` <= '200'
AND `houseconstructiontypelist`.`ConstructionTypeFk` IN('1', '2')

逻辑如下:

find rows where energy_extra.PacketFk is equal to 7 or where energy_extra.house_extra_id is equal to 4

then check all the other conditions

最佳答案

逻辑 OR 运算符的优先级低于 AND 运算符 - 因此您得到了这个结果。

您只需要将您的第一个条件括在方括号中:

WHERE 
(
`energy_extra`.`PacketFk` IN('7')
OR `energy_extra`.`house_extra_id` IN('4')
)
AND ....

关于mysql - WHERE 子句后跟 OR 然后再 WHERE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35628887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com