是否可以在一个查询中执行多个 SQL select 语句?两个选择语句都在同一张表上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15278326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:08:04  来源:igfitidea点击:

Is it possible to do multiple SQL select statements within one query? Both select statements would be on the same table

sqljoin

提问by EGr

This is probably due to my limited SQL knowledge, but I'm attempting to obtain data using a statement like this:

这可能是由于我的 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%'

I realize that is probably a very bad statement, but it has worked so far. In the beginning I exclude entries where hostname isn't like "obl". I have another query like this:

我意识到这可能是一个非常糟糕的陈述,但到目前为止它已经奏效。一开始我排除了主机名不像“obl”的条目。我有另一个这样的查询:

SELECT hostname 
FROM mytable 
WHERE hostname LIKE '%obl%' 
    AND group IN ('group9','group0')

From what I understand, joinswouldn't be used in this case, since both queries are on the same table (although my knowledge of joins is very limited). I'm accessing this database via a webservice, and I'm not certain what kind of database it is.

据我了解,在这种情况下不会使用连接,因为两个查询都在同一个表上(尽管我对连接的了解非常有限)。我通过网络服务访问这个数据库,我不确定它是什么类型的数据库。

Does anyone have any ideas of how I could get the values from both queries in one query?

有没有人知道如何在一个查询中从两个查询中获取值?

回答by Héctor Luaces Novo

You should use a UNION statement.

您应该使用 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')

That will do it, but I think you could rethink those where conditions. I'll give it a thought and edit if necessary.

那会做到这一点,但我认为你可以重新考虑那些条件。如有必要,我会考虑一下并进行编辑。

EDIT:You can achieve what you are trying to with a single query, no need for the 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%'
)
OR (
    hostname LIKE '%obl%' 
    AND group IN ('group9','group0')
)

回答by DevT

you can use unionstatement or ORcondition to combine these two statements.

您可以使用unionstatement 或ORcondition 来组合这两个语句。

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'))

回答by arsuceno

You can combine the two WHERE conditions in just one, using OR to apply both of them. Something like this:

您可以将两个 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')

Two points:

两点:

  • Using uppercase for SQL words makes easier to read the queries.
  • Maybe you will have to use SELECT DISTINCT hostname if you want to be sure about not getting the same one repeated.
  • 对 SQL 单词使用大写字母可以更容易地阅读查询。
  • 如果您想确保不会重复使用相同的主机名,也许您将不得不使用 SELECT DISTINCT 主机名。