title: UNION 2 Select-queries with computed columns
body: <p>I have 4 simple tables containing GUIDs in a database.<br />
  The database schema looks like this:</p>
  <p><a href="https://i.stack.imgur.com/RWSsh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RWSsh.png" alt="Database Schema" /></a></p>
  <p>I have created some example data.</p>
  <pre><code>CREATE TABLE `computermapping` (
    `ComputerMappingID` int(11) NOT NULL,
    `PrinterGUID` char(36) NOT NULL,
    `ComputerGUID` char(36) NOT NULL
  );
  
  INSERT INTO `computermapping` (`ComputerMappingID`, `PrinterGUID`, `ComputerGUID`) VALUES
  (43, 'a353199e-cd02-4fa6-904e-c172235abe9f', '87f44dc7-09e9-483c-935c-325b77ea4355'),
  (44, '5549f63f-e02f-4685-a976-96b50c299bed', '87f44dc7-09e9-483c-935c-325b77ea4355'),
  (80, 'a353199e-cd02-4fa6-904e-c172235abe9f', '4e392a05-e535-457f-abc3-3f22739aa5ca'),
  (90, 'd2e9c5b9-43aa-4713-98e5-deb5e09e461b', '6bc91c0e-9aea-4847-895b-d8064e8feae5'),
  (91, '5da046e3-4fb3-4ca5-89e8-239d81d6dc97', '6bc91c0e-9aea-4847-895b-d8064e8feae5'),
  (92, '4b4a9288-1368-4ec7-a184-638d432d7d28', '5bec3779-b002-46ba-97c4-19158c13001f'),
  (93, '5549f63f-e02f-4685-a976-96b50c299bed', '5bec3779-b002-46ba-97c4-19158c13001f'),
  (94, '957b7233-e590-4e7d-aed6-aee0573fc3a8', '5bec3779-b002-46ba-97c4-19158c13001f'),
  (95, '5106f1f7-068f-463f-9b76-7cc0ba017184', '5bec3779-b002-46ba-97c4-19158c13001f');
  
  CREATE TABLE `computerdefaultprinter` (
    `ComputerGUID` char(36) NOT NULL,
    `PrinterGUID` char(36) NOT NULL
  );
  
  INSERT INTO `computerdefaultprinter` (`ComputerGUID`, `PrinterGUID`) VALUES
  ('5bec3779-b002-46ba-97c4-19158c13001f', '4b4a9288-1368-4ec7-a184-638d432d7d28'),
  ('87f44dc7-09e9-483c-935c-325b77ea4355', 'a353199e-cd02-4fa6-904e-c172235abe9f'),
  ('6bc91c0e-9aea-4847-895b-d8064e8feae5', 'd2e9c5b9-43aa-4713-98e5-deb5e09e461b');
  
  CREATE TABLE `usermapping` (
    `UserMappingID` int(11) NOT NULL,
    `PrinterGUID` char(36) NOT NULL,
    `UserGUID` char(36) NOT NULL
  );
  
  INSERT INTO `usermapping` (`UserMappingID`, `PrinterGUID`, `UserGUID`) VALUES
  (63, '4b4a9288-1368-4ec7-a184-638d432d7d28', 'd3cf699b-8d71-4dbc-92f3-402950042054'),
  (64, 'a353199e-cd02-4fa6-904e-c172235abe9f', 'd3cf699b-8d71-4dbc-92f3-402950042054'),
  (65, 'd2e9c5b9-43aa-4713-98e5-deb5e09e461b', '25c35cf5-ec20-4be5-a7f5-68f6fb93d192'),
  (66, '5549f63f-e02f-4685-a976-96b50c299bed', '5ca9359d-5492-4b22-bd02-209f104e61d3');
  
  CREATE TABLE `userdefaultprinter` (
    `UserGUID` char(36) NOT NULL,
    `PrinterGUID` char(36) NOT NULL
  );
  
  INSERT INTO `userdefaultprinter` (`UserGUID`, `PrinterGUID`) VALUES
  ('d3cf699b-8d71-4dbc-92f3-402950042054', '4b4a9288-1368-4ec7-a184-638d432d7d28'),
  ('5ca9359d-5492-4b22-bd02-209f104e61d3', '5549f63f-e02f-4685-a976-96b50c299bed'),
  ('25c35cf5-ec20-4be5-a7f5-68f6fb93d192', 'd2e9c5b9-43aa-4713-98e5-deb5e09e461b');
  </code></pre>
  <p>Link: <a href="http://sqlfiddle.com/#!9/a9e4ce" rel="nofollow noreferrer">http://sqlfiddle.com/#!9/a9e4ce</a></p>
  <p>At first I want to query for all Printers that should get connected to a Computer. The Computer is identified by its ComputerGUID. (<em>in example: 5bec3779-...</em>)
  I also want to know, which Printer will be the Default-Printer. A Computer can only have one Default-Printer.</p>
  <p>I use the following query:</p>
  <pre><code>SELECT cm.PrinterGUID,
  CASE 
   WHEN cp.PrinterGUID IS NULL THEN 0 
   ELSE 1 
  END AS isDefaultPrinter
    FROM computermapping cm
    LEFT 
    JOIN computerdefaultprinter cp 
      ON cm.ComputerGUID = cp.ComputerGUID 
     AND cm.PrinterGUID = cp.PrinterGUID 
   WHERE cm.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
   ORDER 
      BY cm.PrinterGUID
  </code></pre>
  <p>I get the follwoing result, which is OK:</p>
  <pre><code>PrinterGUID                          | isDefaultPrinter
  ---------------------------------------------------------
  4b4a9288-1368-4ec7-a184-638d432d7d28 | 1
  5106f1f7-068f-463f-9b76-7cc0ba017184 | 0
  5549f63f-e02f-4685-a976-96b50c299bed | 0
  957b7233-e590-4e7d-aed6-aee0573fc3a8 | 0
  </code></pre>
  <p>.</p>
  <p>Second I have the same for users:</p>
  <pre><code>SELECT usermapping.PrinterGUID,
  CASE 
   WHEN userdefaultprinter.PrinterGUID IS NULL THEN 0 
   ELSE 1 
  END AS isDefaultPrinter
  FROM usermapping
  LEFT JOIN userdefaultprinter ON usermapping.UserGUID = userdefaultprinter.UserGUID AND usermapping.PrinterGUID = userdefaultprinter.PrinterGUID 
  WHERE usermapping.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054'
  ORDER BY usermapping.PrinterGUID
  </code></pre>
  <p>Result:</p>
  <pre><code>PrinterGUID                          | isDefaultPrinter
  ---------------------------------------------------------
  4b4a9288-1368-4ec7-a184-638d432d7d28 | 1
  a353199e-cd02-4fa6-904e-c172235abe9f | 0
  </code></pre>
  <p>.</p>
  <p>This is all OK. To simplify my program, I want to replace those 2 query's by a single SQL-query.
  The desired result would look like this:</p>
  <pre><code>PrinterGUID                          | isComputerDefaultPrinter | isUserDefaultPrinter
  ----------------------------------------------------------------------------------------
  4b4a9288-1368-4ec7-a184-638d432d7d28 | 1                        | 1
  5106f1f7-068f-463f-9b76-7cc0ba017184 | 0                        | 0
  5549f63f-e02f-4685-a976-96b50c299bed | 0                        | 0
  957b7233-e590-4e7d-aed6-aee0573fc3a8 | 0                        | 0
  a353199e-cd02-4fa6-904e-c172235abe9f | 0                        | 0
  </code></pre>
  <p>The PrinterGUID should be Unique in the result. Of course the User Default-Printer may not be the same as the Computer Default-Printer like in the example above.</p>
  <p><strong>EDIT:</strong>
  Unfortunately my know-how in SQL is limited. As of my research using a UNION would be suitable to join two selects. I started to build up a query using the above example as base. This is the query I ended up with, but the result is not as desired.</p>
  <pre><code>SELECT computermapping.PrinterGUID,
  CASE 
   WHEN computerdefaultprinter.PrinterGUID IS NULL THEN 0 
   ELSE 1 
  END AS isComputerDefaultPrinter,
  null AS isUserDefaultPrinter
  FROM computermapping 
  LEFT JOIN computerdefaultprinter ON computermapping.ComputerGUID = computerdefaultprinter.ComputerGUID AND computermapping.PrinterGUID = computerdefaultprinter.PrinterGUID 
  WHERE computermapping.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
  
  UNION
  
  SELECT usermapping.PrinterGUID,
  null AS isComputerDefaultPrinter,
  CASE 
   WHEN userdefaultprinter.PrinterGUID IS NULL THEN 0 
   ELSE 1 
  END AS isUserDefaultPrinter
  FROM usermapping
  LEFT JOIN userdefaultprinter ON usermapping.UserGUID = userdefaultprinter.UserGUID AND usermapping.PrinterGUID = userdefaultprinter.PrinterGUID 
  WHERE usermapping.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054'
  </code></pre>
  <p>Could you please help me to solve this?</p>
  <p>Thank you!</p>