sql >> Database teknologi >  >> RDS >> Mysql

UNION 2 Select-forespørgsler med beregnede kolonner

Jeg tror, ​​at nøgleideen er at få listen over printere, du ønsker:

select PrinterGUID
from ((select cm.PrinterGUID
       from computermapping cm
       where cm.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
      ) union -- to remove duplicates
      (select PrinterGUID
       from usermapping um
       where um.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054'
      )
     ) p;

Så kan du bruge exists for at se, om disse er standardindstillingerne:

select p.PrinterGUID,
       (exists (select 1
                from computerdefaultprinter cdp
                where cdp.PrinterGUID = p.PrinterGUID and
                      cdp.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
               )
       ) as is_computer_default,
       (exists (select 1
                from userdefaultprinter udp
                where udp.PrinterGUID = p.PrinterGUID AND
                      udp.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054'
               )
      ) as is_user_default
from ((select cm.PrinterGUID
       from computermapping cm
       where cm.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
      ) union -- to remove duplicates
      (select PrinterGUID
       from usermapping um
       where um.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054'
      )
     ) p;

Her er en db<>violin.



  1. Sådan tilføjes en filgruppe til en SQL Server-database (T-SQL)

  2. Python:Henter PostgreSQL-forespørgselsresultater som formaterede JSON-værdier

  3. Oracle Sequence Transactionality

  4. Mysql-kryptering / lagring af følsomme data,