Can I make multiple SQL select statements in a single query? Both select statements will be in the same table.

This is probably due to my limited knowledge of SQL, but I'm trying to get data using an instruction like this:

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 understand that this is probably a very bad statement, but it still works. In the beginning, I exclude entries in which the hostname does not look like "obl." I have another request:

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

From what I understand, join will not be used in this case, since both queries are in the same table (although my knowledge of joins is very limited). I access this database through webservice, and I'm not sure what the database is.

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

+5
3

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

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

:

  • SQL- .
  • , SELECT DISTINCT, , .
0

All Articles