这可能是由于我的SQL知识有限,但我试图使用这样的语句来获取数据:
SELECT hostname
FROM mytable
WHERE hostname NOT LIKE '%obl%'
AND group NOT IN ('group1','group2','group3','group4','group5','group6','group7')
AND osname LIKE '%Windows%'
AND hostname NOT LIKE 'nic%'
我意识到这可能是一个非常糟糕的陈述,但到目前为止它已经奏效了。在开始我排除主机名不像“obl”的条目。我有另一个这样的查询:
SELECT hostname
FROM mytable
WHERE hostname LIKE '%obl%'
AND group IN ('group9','group0')
根据我的理解,在这种情况下不会使用 join ,因为两个查询都在同一个表上(尽管我对连接的了解非常有限)。我通过网络服务访问这个数据库,我不确定它是什么类型的数据库。
有没有人对如何在一个查询中获取两个查询的值有任何想法?
答案 0 :(得分:8)
您应该使用UNION语句。
SELECT hostname FROM mytable
WHERE hostname
NOT LIKE '%obl%' AND
group NOT IN ('group1','group2','group3','group4','group5','group6','group7')
AND osname LIKE '%Windows%'
AND hostname not LIKE 'nic%'
UNION
SELECT hostname FROM mytable
WHERE hostname
LIKE '%obl%'
AND group in ('group9','group0')
那会这样做,但我想你可以重新考虑那些有条件的人。如有必要,我会仔细考虑并编辑。
编辑:您可以通过单个查询实现您的目标,无需联合。
SELECT hostname FROM mytable
WHERE (
hostname NOT LIKE '%obl%'
AND group NOT IN ('group1','group2','group3','group4','group5','group6','group7')
AND osname LIKE '%Windows%'
AND hostname NOT LIKE 'nic%'
)
OR (
hostname LIKE '%obl%'
AND group IN ('group9','group0')
)
答案 1 :(得分:0)
您可以使用union
语句或OR
条件来合并这两个语句。
select hostname from mytable where (hostname not like '%obl%' and group not in ('group1','group2','group3','group4','group5','group6','group7') and osname like '%Windows%' and hostname not like 'nic%')
OR
(hostname like '%obl%' and group in ('group9','group0'))
答案 2 :(得分:0)
您可以将两个WHERE条件合并为一个,使用OR来应用它们。像这样:
SELECT hostname FROM mytable WHERE
(hostname NOT LIKE '%obl%' AND group NOT IN ('group1','group2','group3','group4','group5','group6','group7') AND osname LIKE '%Windows%' AND hostname NOT LIKE 'nic%')
OR
(hostname LIKE '%obl%' AND group IN ('group9','group0')
两点: