SQL-Query - 使用其他动态列连接多个表

时间:2018-02-09 09:04:45

标签: mysql sql

我需要帮助来构建SQL语句。 数据库架构如下所示:

SQL-Schema

我确实准备了以下 SQL-Fiddle ,其中包含示例数据: http://sqlfiddle.com/#!9/831bab/1

对于SQL-Fiddle中的示例数据,我想查询计算机名“Client02”并希望得到以下结果:

通缉结果1

PrinterName   | PrintServer | PrinterActive | ComputerActive | isDefaultPrinter
PRT01_Zentral | DC01        | True          | True           | False
PRT02_BH      | DC01        | True          | True           | True

对于SQL-Fiddle中的示例数据,我想查询计算机名“Client01”并希望得到以下结果:

通缉结果2

PrinterName   | PrintServer | PrinterActive | ComputerActive | isDefaultPrinter
PRT01_Zentral | DC01        | True          | True           | True

如您所见,我需要加入所有表并添加类似帮助列的内容,其中包含有关默认打印机的信息。 (真/假)

我开始构建查询,但我不知道如何继续......

SELECT printers.PrinterName, printers.PrintServer, printers.PrinterActive
FROM computermapping
LEFT JOIN computers ON computermapping.ComputerID = computers.ComputerID
LEFT JOIN printers ON computermapping.PrinterID = printers.PrinterID
LEFT JOIN computerdefaultprinter ON computers.ComputerID = computerdefaultprinter.ComputerID
WHERE computers.ComputerName = "Client02" 

我认为我的请求包含所需的所有信息。 SQL-Fiddle具有样本数据,可以轻松地重现它。 WantedResults应该清楚地显示目标。

修改
DBMS:MySQL

2 个答案:

答案 0 :(得分:0)

我所做的是在你的查询中添加一个if语句,将内部结果(只能是1)与你的outter选择进行比较。有一种更好的方法可以达到这个目的,但这个答案反映了你所问的查询结果。

编辑:我假设每台计算机只有一台默认打印机。 你确定想要的结果2是否正确?因为在数据库中,记录表示computer1有1个printerid(1)是默认值,而computer2有2个打印机(1和2),2是默认值。

SELECT printers.PrinterName,
       printers.PrintServer,
       printers.PrinterActive,
       IF (
             (SELECT computerdefaultprinter.printerid
              FROM computerdefaultprinter
              WHERE computerdefaultprinter.computerid = computermapping.computerid) = computermapping.printerid,
           'true',
           'false') AS isDefaultPrinter
FROM computermapping
LEFT JOIN computers ON computermapping.ComputerID = computers.ComputerID
LEFT JOIN printers ON computermapping.PrinterID = printers.PrinterID
LEFT JOIN computerdefaultprinter ON computers.ComputerID = computerdefaultprinter.ComputerID
WHERE computers.ComputerName = "Client02"

答案 1 :(得分:0)

您可以使用以下查询来获取所需的结果

SELECT printers.PrinterName, 
       printers.PrintServer, 
       printers.PrinterActive, 
       computers.ComputerActive, 
       CASE 
         WHEN computerdefaultprinter.PrinterID IS NULL THEN "false" 
         ELSE "true" 
       END AS isDefaultPrinter 
FROM   computermapping 
       LEFT JOIN computers 
              ON computermapping.ComputerID = computers.ComputerID 
       LEFT JOIN printers 
              ON computermapping.PrinterID = printers.PrinterID 
       LEFT JOIN computerdefaultprinter 
              ON computers.ComputerID = computerdefaultprinter.ComputerID 
                 AND printers.PrinterID = computerdefaultprinter.PrinterID 
WHERE  computers.ComputerName = "Client02" 

我只添加了 CASE ... WHEN 语句并修改了原始查询的连接条件。 但我认为不是使用表 computerdefaultprinter ,为什么不将列 IsDefaultPrinter 添加到表 computermapping < / em>的

CREATE TABLE `computermapping` (
  `ComputerMappingID` int(11) NOT NULL,
  `PrinterID` int(11) NOT NULL,
  `ComputerID` int(11) NOT NULL,
  `IsDefaultPrinter` int(1) NOT NULL DEFAULT 0,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;