我在下面有以下sql查询。我对此sql查询未选择的所有记录感兴趣。我在下面尝试过,但我的SQL语法有错误。有人可以建议吗?
SELECT *
FROM codec_status NOT IN
(SELECT *
FROM codec_status
WHERE version_er_r='0'
OR ntpStatus_er_r='0'
OR stAvailable_er_r='0'
OR RXRate_er_r='0'
OR TXRate_er_r='0'
OR DefaultCallRate_er_r='0'
OR RemoteView_er_r='0'
OR VoiceVlan_er_r='0'
OR Transport_er_r='0'
)
答案 0 :(得分:1)
你应该学习逻辑门,在这些条件下帮助你。这里只需使用De Morgan's Theorem
SELECT * FROM codec_status NOT IN (SELECT * FROM codec_status WHERE version_er_r='0' OR ntpStatus_er_r='0' OR stAvailable_er_r='0' OR RXRate_er_r='0' OR TXRate_er_r='0' OR DefaultCallRate_er_r='0' OR RemoteView_er_r='0' OR VoiceVlan_er_r='0' OR Transport_er_r='0')
将其替换为
SELECT * FROM codec_status WHERE version_er_r<>'0' AND ntpStatus_er_r<>'0' AND stAvailable_er_r<>'0' AND RXRate_er_r<>'0' AND TXRate_er_r<>'0' AND DefaultCallRate_er_r<>'0' AND RemoteView_er_r<>'0' AND VoiceVlan_er_r<>'0' AND Transport_er_r<>'0'
答案 1 :(得分:0)
让我们说来自表codec_status的主要ID只是id。然后你应该这样做:
SELECT * FROM codec_status where id NOT IN
(SELECT id FROM codec_status WHERE version_er_r='0' OR ntpStatus_er_r='0' OR stAvailable_er_r='0' OR RXRate_er_r='0' OR TXRate_er_r='0' OR DefaultCallRate_er_r='0' OR RemoteView_er_r='0' OR VoiceVlan_er_r='0' OR Transport_er_r='0')
答案 2 :(得分:0)
有些DBMS有MINUS运算符:
select * from tablename where ....
minus
select * from tablename where ....
遗憾的是,MySQL不支持减运算符。如果你的表有一个ID列,你可以这样写:
select * from codec_status
where
id NOT IN (select ID
from
codec_status
where ....)
(使用NOT IN时要小心,特别是如果子查询返回NULL值,但如果ID是主键,则应该没有问题)。
无论如何,您可以将查询编写为:
select * from codec_status
where NOT (
version_er_r='0'
OR ntpStatus_er_r='0'
OR stAvailable_er_r='0'
OR RXRate_er_r='0'
OR TXRate_er_r='0'
OR DefaultCallRate_er_r='0'
OR RemoteView_er_r='0'
OR VoiceVlan_er_r='0'
OR Transport_er_r='0'
)
即使在这里你必须要小心空值(如果所有列都为null你想在第一个查询或第二个查询中返回行?)
答案 3 :(得分:0)
只需使用not
SELECT *
FROM codec_status
WHERE NOT
(version_er_r='0'
OR ntpStatus_er_r='0'
OR stAvailable_er_r='0'
OR RXRate_er_r='0'
OR TXRate_er_r='0'
OR DefaultCallRate_er_r='0'
OR RemoteView_er_r='0'
OR VoiceVlan_er_r='0'
OR Transport_er_r='0')