gpt4 book ai didi

mysql - "AND"和 "OR"关键字如何在选择查询中一起使用?

转载 作者:行者123 更新时间:2023-11-29 06:19:24 24 4
gpt4 key购买 nike

T 有一个选择查询

 SELECT * FROM tbl_docuserstatus WHERE doc_id='1' 
AND doc_midlvluser = '1' AND
(docstatus_id != '4' OR docstatus_id != '5' OR docstatus_id != '8' OR docstatus_id != '0')

但结果似乎不适用于 (docstatus_id != '4' OR docstatus_id != '5' OR docstatus_id != '8' OR docstatus_id != '0') 部分。

我需要查询显示 docid = 1 和 doc_midlvluser = 1 且 dostatus_id shld 不等于 4 或 5 或 8 或 0 的所有值

请帮助我应该如何使用?

最佳答案

用途:

SELECT * FROM tbl_docuserstatus WHERE doc_id='1' 
AND doc_midlvluser = '1' AND
(docstatus_id != '4' AND docstatus_id != '5'
AND docstatus_id != '8' AND docstatus_id != '0')

或者只应用德摩根定理,这样更容易编码和阅读:

SELECT * FROM tbl_docuserstatus WHERE doc_id='1' 
AND doc_midlvluser = '1' AND
NOT (docstatus_id = '4' OR docstatus_id = '5'
OR docstatus_id = '8' OR docstatus_id = '0')

或者更好的是,使用 IN:

SELECT * FROM tbl_docuserstatus WHERE doc_id='1' 
AND doc_midlvluser = '1' AND
docstatus_id NOT IN ('4','5','8','0')

相关:https://stackoverflow.com/questions/130965/what-is-the-worst-code-youve-ever-written/311377#311377

还有这个:Is it acceptable to only use the 'else' portion of an 'if-else' statement?

关于mysql - "AND"和 "OR"关键字如何在选择查询中一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4615113/

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